Previously in Windows Phone 8.0 apps, we could navigate deeper to the same page this way:
NavigationService.Navigate(new Uri("/SamePage.xaml", UriKind.Relative));
Page was cached automatically, so after navigating back, user was on the same position on the list when he left.
But in Windows Phone Store Apps we navigate deeper to the same page this way:
Frame.Navigate(typeof(SamePage), id);
But after navigating back it loads data again so if user was on the middle of a long list, now he is on the top:
private async void NavigationHelper_LoadState(object sender, LoadStateEventArgs e)
{
// TODO: Create an appropriate data model for your problem domain to replace the sample data.
var group = await SampleDataSource.GetGroupAsync((string)e.NavigationParameter);
this.DefaultViewModel["Group"] = group;
}
How can I cache a page like the way it was done previously so user will be on the same position on a list where he left?
(I included Windows apps too cause they are familiar with it from a longer time).
In your page constructor you'll have to specify
public MainPage()
{
this.InitializeComponent();
this.NavigationCacheMode = NavigationCacheMode.Required;
}
In App.cs you can set RootFrame.CacheSize which hints the OS how many pages it should try to keep in cache.
Also you probably shouldn't reset the datacontext in NavigationHelper_LoadState - this method is called each time you navigate to the page, even if you navigate back.
Related
Here's the scenario for my page navigation:
MainPage (MP) <==> Locations (L) <==> AddLocation (AL)
I don't want the user to ever go to AddLocation when hitting the back button on the phone.
If they are on MP and they hit back, they should exit the app.
If they went MP->L and hit back, they should go to MainPage.
If they went MP->L->AL and hit back, they should go to Locations.
If they went MP->L->AL->L and hit back, they should go to MainPage and not back to AddLocations.
Right not, I have the standard nav helpers in the Common folder and then I've added this code to the Locations page to make this happen:
protected override void OnNavigatedTo(NavigationEventArgs e)
{
this.navigationHelper.OnNavigatedTo(e);
var bs = Frame.BackStack.Where(b => b.SourcePageType.Name == "MainPage").FirstOrDefault();
if (bs!= null)
{
Frame.BackStack.Clear();
Frame.BackStack.Add(bs);
}
}
This seems to be a terrible hack to me and I'm sure there some supported/designed way to do this that I don't know about. I'm very new to WinRT and Xaml.
Your problem is went MP->L->AL->L and hit back, they should go to MainPage and not back to AddLocations isn't it?
The solution is: when you finish adding location, you should go to Locations Page from AddLocation Page.
But you should not use Frame.Navigate(typeof(LocationPage)); to do this.
You should use Frame.GoBack();, So AddLocation Page will be removed from BackStack auto.
My first C#/Xaml-experience was Windows Phone 8 where navigation worked like this:
NavigationService.Navigate(new Uri("/MyPage.xaml", UriKind.Relative));
Which meant that I could replace the String "/MyPage.xaml" by anything I needed and go to that page.
Is something similar possible with Windows Store Apps?
this.Frame.Navigate(typeof(MyPage), UriKind.Relative);
MyPage isn't a string here so I can't simply replace it while the app is running. Still any way to do this?
What I am doing is:
I use a ListBox that gets it's data from a bound viewModel. There I wanted to store the target where the app should navigate to after a SelectionChanged Event is fired.
You can use it like below if you get the string with namespace.
this.Frame.Navigate(Type.GetType("Namespace.PageName"));
Yes you can pass the url of the page on selection changed and load the corresponding dataContext
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
base.OnNavigatedTo(e);
var title = NavigationContext.QueryString["title"];
(DataContext as ImagePageViewModel).Load(title);
}
Follow this example which would fulfill your requirement
page-navigation-in-windows-phone-and-windows-8
I have an application for Windows 8 with a page (Frame) for displaying a list of items and a page for downloading & displaying the items details. I am also using MVVM Light for sending notifications.
Application use goes something like this:
Open Main Page
Navigate to List Page
Frame.Navigate(typeof(MyPage));
Choose Item
//Complete logic
Frame.GoBack();
Back on Main Page, I start downloading the file in the view model, I send ONE NotificationMessage saying BeginDownloadFile and after it is downloaded ONE NotificationMessage saying EndDownloadFile.
The first time I do steps 2,3, & 4 my NotificationReceived method is hit once, the second twice and so forth.
private async void NotificationMessageReceived(NotificationMessage msg)
{
if (msg.Notification == Notifications.BeginDownloadFile)
{
FileDownloadPopup.IsOpen = true;
}
else if (msg.Notification == Notifications.EndDownloadFile)
{
FileDownloadPopup.IsOpen = false;
}
}
Additional information: I only have one FileDownloadPopup, yet each time, an additional popup is shown each time the NotificationMessageReceived method is called.
My only conclusion is that between navigating forwards and backwards in my app, there are multiple MainPages being created and never closed. This results in many NotificationsMessageReceived methods just waiting for a notification to come their way so they can show their popup.
I have two questions:
1. Does this sound like normal behaviour for a Windows 8 app?
2. How can I close all instances of the MainPage or return to the previous instance without creating a new instance?
Please let me know if I have missed something important out before marking my question down.
This sounds normal to me. The default navigation behaviour in Windows 8 is to create a new page instance each time you navigate to a new page, regardless of whether this is forward or back navigation.
Try setting the NavigatinCacheMode on MainPage to Required. See the MSDN documentation for details of how page caching works.
It sounds like you are registering eventhandlers in the page and then not removing them. Each time you navigate to the page again the handler is being added again in addition to the one you previously added. Try to add your event handler in OnNavigatedTo, and make sure you unregister it in OnNavigatedFrom.
protected override void OnNavigatedTo(Windows.UI.Xaml.Navigation.NavigationEventArgs e)
{
MyEvent.OnDownloadRequest += MyLocalDOwnloadHandler; // add the handler
}
protected override void OnNavigatedFrom(Windows.UI.Xaml.Navigation.NavigationEventArgs e)
{
MyEvent.OnDownloadRequest -= MyLocalDOwnloadHandler; // remove the handler
}
I am working on a Windows Phone application, here is the scenario that I have problem:
So I have three pages, lets call it page 1, 2, 3.
In page 1, I have a button called start downloading. Click the button and use NavigateService.Navigate(page2Uri) and navigate to page2.
Page 2 makes query and downloads images from internet, so in its OnNavigateTo handler, I check the page back stack, if it is navigated from page 1, I will do the download. In the app bar of this page, I have a button that can navigate to page3.
Page 3 is a list of options that will perform some behavior on the image that is downloaded in page2. Once I choose an option, I want to go back to page 2 and perform some behavior on the loaded image.Here the question comes: if I use NavigateService.Navigate(page2Uri) to navigate from page3 to page2, it will call the Page2 constructor and OnNavigateTo handler again, which will cause it to lose every instance variable it already got.
But if I use NavigatService.GoBack it will go back to page2, then realizes that the backstack top entry is page1 (since page1 -> page2 -> page3). So it will re-download everything again.
I dont want anything to be downloaded again when navigate back form page3 to page2. So wondering if anyone has good idea about this.
Thank you.
You can use the query parameters and NavigationEventArgs to help.
First, you can use the NavigationEventArgs to determine if the user is going forward or background by checking the NavigationMode.
Second, you can tell page 2 to download by using the query parameters.
From page1:
private void MoveToPage2FromPage1()
{
NavigationService.Navigate(new Uri("/Page2.xaml?shouldDownload=true", UriKind.Relative));
}
and page2:
protected override void OnNavigatedTo(NavigationEventArgs e)
{
if (e.NavigationMode == NavigationMode.Back) return;
string shouldDownload = ""; //May not be needed if you'll only ever go to page 2 from page 1 to download...
if (NavigationContext.QueryString.TryGetValue("shouldDownload", out shouldDownload))
{
Convert.ToBoolean(shouldDownload);
}
}
There are several ways to pass data to another page:
You can use query parameters as Shawn suggested.
You can use global data stored somewhere like in app.cs
You can use a static class to hold the data.
You can use a shared viewModel to hold the parameters. (or static properties in the viewmodel)
It all depends on the particular case. I think Shawns suggestion of using query paramaters is probably the most 'correct' MVVM way, but the other methods have their place.
You need to implement the following function and the navigation service.
These code will definitely solve your problem
for two or more parameters, use this code
String download="true";
String file="image";
NavigationService.Navigate(new Uri("/Page3.xaml?download="+download+"&file="+file+"", UriKind.Relative));
OnNavigatedTo, add the following code on to your Page2
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
String download=NavigationContext.QueryString["download"];
String file=NavigationContext.QueryString["file"];
}
For the above OnNavigatedTo function outputs true and image. You can use MessageBox.Show(); to output
For example, I want a button to take me to a settings page, and then back to the main screen. Just as an example for me to understand the workflow. Any guidance?
I use the NavigationService to navigate to the new page, where the Uri has a path relative to the project's base directory.
private void OptionsMenuItem_Click(object sender, EventArgs e)
{
// Navigate to the new page
NavigationService.Navigate(new Uri("/Views/OptionsView.xaml", UriKind.Relative));
}
The back button on the phone will take the user back to the previous page automatically, or you could code you own return button using the NavigationService again.
Dr. Herbie's method works great.
Another option is to implement INavigate on your PhoneApplicationPage. Then use a HyperlinkButton. If you have a lot of buttons and don't want to write a bunch of click handlers, this can be more convenient.
Your implementation of INavigate.Navigate just uses the page's NavigationService like this:
public bool Navigate(Uri source)
{
NavigationService.Navigate(source);
return true;
}
You must take care of certification requirements! Look at this tutorial: http://www.yourwindowsphone7.com/tutorials/navigation-in-windows-phone-7-apps.html