How not to reload CollectionView after clicking on Back button - xamarin

Hi I'm facing a problem with my small xamarin app.
I have my small xamarin-form app that everytime the user enter a product Id click OK button retrieves a single (data related to that product Id only) data thru Web API from page A, and then pass it to a collectionview in another page B.
then there is a back button to go back to page A and the user must enter another product Id again and the app must fetch data thru web api again and pass it again to page B in the collectionview.
So the problem is now, when I click on back button in Page B, it's clear the collectionview data, whereas I need the collectionview to keep the state even If I pop back.
I'm not using any MVVM pattern or complicated stuff from now as I'm new to xamarin; it's just a simple architecture.
Or maybe I'm using wrong technics to achieve this and can advise me a better way to achieve that. I'm please open to your suggestion !!!
In brief, I need to be able not to reload CollectionView after clicking on Back button.
Below is the code that Bind to the View on Page B
public async void OnGetProductDetailButton(object sender, EventArgs e)
{
await Navigation.PushAsync(new ProductDetailPage
{
BindingContext = productDetails
});
}

Related

Different Types of Navigation in Xamarin Forms

What is the difference between these two Navigation paradigms in Xamarin.Forms?
await Navigation.PushModalAsync(new Dashboard());
await Navigation.PushModalAsync(new NavigationPage(new Dashboard()));
What is a Modal Page
Navigation.PushModalAsync will cause the new Xamarin.Forms.Page to appear Modally, meaning its animation will start from the bottom of the screen, covering the previous current page and it will not contain a back-button.
On iOS, a user will not be able to swipe-left to return to the previous page.
Android users are able to use the hardware back button to dismiss a modal page.
Modal pages are useful when you want the user to make a conscious decision to dismiss the page.
Example
When an iOS user is filling out a form and they swipe left to go back, does it save the form or discard the form? It is unclear to the user. To make the UX more intuitive, you should display the form modally
UI Differences
Using await Navigation.PushModalAsync(new Dashboard()); will display the Dashboard Page Modally, but the new Page will not have a NavigationBar.
Using await Navigation.PushModalAsync(new NavigationPage(new Dashboard())); will also display the Dashboard Page Modally, but the new Page will have a NavigationBar.
Sample App Source Code: https://github.com/brminnick/XamList
Edit
#AlessandroCaliaro and I had a good discussion in the comments of his answer, below:
It's important to note that Xamarin.Forms.INavigation uses two different stacks, ModalStack and NavigationStack. Navigation.PushModalAsync adds a Page to the ModalStack and Navigation.PushAsync adds a Page to the NavigationStack.
And to pop a Page from the ModalStack, you need to use Navigation.PopModalAsync(), whereas you would use Navigation.PopAsync() to remove a Page from the NavigationStack.
The first, push a page in a "modal" way (without navigation bar on the top)
the second... is an error because you try to add a navigationpage to a navigationstack, but I think it is not possible....

Xamarin Forms List View Show Different Pages?

New to Xamarin here. How can I make each item on a listview go to a different page in Xamarin Forms?
For example, something like Facebook app:
Facebook is using a master detail page for this. Your should take a look at this Xamarin link about Master Detail Page. Let me know if this is not what you're looking for and I'll give you a solution for a listview. I personally would go with the master detail page.
The main flow is simple. You have your ListView, you attach an event on the ItemTapped to it, in code behind in the ItemTap handle method you can access which item was tapped and with that information you decide where you want to navigate.
void Handle_ItemTapped(object sender, ItemTappedEventArgs e)
{
// Item tapped
var item = e.Item as YourItemThatWasBinded;
if (item == null) return;
// Choose the page to navigate to based on the item selected.
// You decide what logic use inside this method if can be as simple using IFs.
var page = GetPageToNavigateWithItem(item);
// Navigate
Navigation.PushAsync(page);
}

How do I create a dynamic page based on a selection?

I am trying to dynamically fill a second panorama page based on what item was selected from the home application screen.
On the application's first start screen there is a listbox if items each with text. If a user taps on an item with text "foobar" a template page should load and the title of the template page should be set to "foobar" and this second panorama page should know that it's data should be related to "foobar".
Is there anyway to do this?
I currently have my MainPage navigate to a new page (DynamicPage.xaml). This navigation is triggered when a ListBox_SelectionChanged event occurs. I have the title text of the DynampicPage.xaml Binding to a TitleText variable that is located in MainPage.xaml.cs. However, when I do this the title of DynamicPage.xaml is ever only set to my initialization value for the titleText variable even though I am updating this variable right before I navigate to the page.
If anyone can provide some help I would be very grateful as I am just a beginner on the WP7 platform. Thanks!
The Binding you're using for the title is only going to update if the TitleText property is a dependency property or if your MainPage is implementing the INotifyPropertyChanged interface so your class can notify the UI when one of its properties changed.
http://windowsphonegeek.com/articles/All-about-Dependency-Properties-in-Silverlight-for-WP7
http://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged(v=vs.95).aspx
But I think this is not the best way for you to achieve this. For now a much better way is to store your data somewhere in a static class, in the main page's constructor load these data into the listbox, and when the user selected an item, navigate the user to the second page like this:
NavigationService.Navigate(new Uri("/DynamicPage.xaml?Item=" + selectedItem.Id, UriKind.Relative));
When you navigate like this a new instance of DynamicPage is created, and in the OnNavigatedTo method you can access the navigation parameters and populate your page with the selected data. For example:
<controls:Panorama x:Name="MyPanorama" Title="TitleHere">...</controls:Panorama>
Item selectedItem = StaticData.GetItem(NavigationContext.QueryString["Item"]);
MyPanorama.Title = selectedItem.Name.ToUpper();
Description.Text = selectedItem.Description;
This way you can use secondary tiles and toast notifications to directly point to a specific content in your application.
If you're getting to understand the navigation you should definitely use the pattern called Model-View-ViewModel which is about to solve these problems mostly with bindings, but trust me, probably this is the easier way for now.

Get results from target page when returning to source page

My Windows Phone 7 app as a page with an edit form with a ListPicker control. One of the items in the list is "add new" which, when selected, opens another page with another edit form for adding a new lookup value. Problem is, page navigation is asynchronous, so when the source page navigates to the target page, code execution continues and I don't know how to get a notification from the target page when the user saves. I want the value they just added to be inserted and selected in the ListPicker on the source page. I'm not even sure how to look this up on Google.
On your first page you will need to override the OnNavigatedTo method (or attach a second event handler)
protected override void OnNavigatedTo(NavigationEventArgs e) {
// Check for state
}
Your second page will need to write a piece of state into a location both pages can access, then the first page can see if this state exists.
A simple Dictionary<string,object> off the static App object can be a starting point for this sort of state.

ApplicationBar is destroyed when canceling back navigation

I have a page with "internal navigation". That means that I show some list on that page and when user picks an item, I (download some data and) repopulate that list.
I made my own history stack, so when user wants to go back, I repopulate the list from history stack. User can go back by flicking or by clicking on hw back button.
Flicking works ok, but back button is weird.
I am canceling the back button event and instead I run my back navigation history. So I am still on the same page. BUT the back button click hides the application bar (even though I am canceling that event). And when I click it again and debug it, the ApplicationBar property is null.
// this overriden method causes ApplicationBar being hidden (or destroyed)
protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
{
e.Cancel = true;
Messenger.Default.Send(...some notification here...); // this runs the internal navigation
ApplicationBar.IsVisible = true; // this doesn't help and on the second try, it throws NullReferenceException
}
// this method is ok, repopulating is working without any problem
private void GestureListener_Flick(object sender, FlickGestureEventArgs e)
{
Messenger.Default.Send( ...some notification here... ); // this run exactly same internal navigation
}
So question is - how to have ApplicationBar not destroyed/hidden? What is back button doing, when I cancel the navigation (it must do something with the AppBar)?
OK, this happens only when I use BindableApplicationBar and its Extensions (from here maxpaulousky.com).
It happens because Extensions handles back button event on page itself.
Solution was to check the Cancel property, and destroy it only when it's false (in BindableApplicationBar class in the Extensions).

Resources