2

I have 2 pages in my application, A and B.

If I'm navigation from the outside of the application to A, I want to display a message box. If I'm navigation from B to A, I don't want to display anything.

Is there any way to identify in A the page which initiated navigation? i.e in A.Loaded (or any other event) I need something like

if(pageFromWhichIAmComingFrom == B) 

OnNavigatedTo, OnNavigationFrom and OnNavigatedFrom don't seem to help me.

2
  • Is A your start page for the app? Commented Apr 8, 2013 at 13:29
  • Yes, A is my start page. Commented Apr 8, 2013 at 14:13

1 Answer 1

3

You could use the PhoneApplicationService class to store information about what page you were on last. For example, use OnNavigatedFrom on Page A:

void OnNavigatedFrom(object sender, Eventargs e)
{
   PhoneApplicationService.Current.State["LastPage"] = "PageA";
}

And then check for that on the next page:

void OnNavigatedTo(object sender, Eventargs e)
{
   if(PhoneApplicationService.Current.State["LastPage"].ToString() == "PageA")
   {
      // came from page A
   }
   else 
   {
       // came from a different page
   }
}

Hope this helps!

UPDATE:

One more thing I just saw that might be worth trying is using the NavigationService.BackStack property. I haven't tried this, but it seems like it should work. In your OnNavigatedTo event handler, you should be able to get the last entry from the stack to see your last page. This would be simpler and wouldn't require you to set any properties manually. Example:

protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
    base.OnNavigatedTo(e);

    var lastPage = NavigationService.BackStack.FirstOrDefault();
}

Found here.

Sign up to request clarification or add additional context in comments.

3 Comments

It worked! I ended up using your first example! I'm not using "normal" navigation (with the navigation service), instead I'm using a centralized navigation service based on MVVM messaging PhoneApplicationFrame Navigation (so the approach with Navigation BackStack does not work for me, I already tried it).
To anyone else being interested, remember to first check if the required key exists in Current.State (if (PhoneApplicationService.Current.State.ContainsKey(InternalNavigationPageLinks.LastPageKey))) - and I would recommend using a static class with constats describing your pages and always use String.CompareOrdinal (or String.Compare) to correctly compare strings.
Glad it helped, and great point(s)! I forgot to add that tidbit about checking ContainsKey.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.