Search code examples
c#.netsilverlightwindows-phone-7

Windows Phone Page Navigation


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.


Solution

  • 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.