Windows Phone Page Navigation
Solution 1
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.
Solution 2
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);
}
}
Related videos on Youtube
Allan Jiang
Updated on October 16, 2022Comments
-
Allan Jiang about 1 year
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 useNavigateService.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 useNavigateService.Navigate(page2Uri)
to navigate from page3 to page2, it will call the Page2 constructor andOnNavigateTo
handler again, which will cause it to lose every instance variable it already got.
But if I useNavigatService.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.
-
Allan Jiang over 11 yearsThank you Shawn, I like your answer and this should be the standard and correct way of dealing with this kind of problem. But I used Jon's idea to keep a global status of the pages, and it can also solve the problem perfectly. +1 for your brief answer.
-
Shawn Kendrot about 10 yearsBe careful about static/global properties. They will not persist when the application tombstones. Make sure you save the global data on a regular basis (or at a minimum in the application events)