How to get previous (referer) scene/page?

Hello,

I'm working with WPF and have encountered a problem:

I want to execute different methods on a page's onLoaded event depending on the previous page. All navigation is done by the Navigate("page.xaml" ) method. How can I do it

My idea is something like this:

<C# code>

if (referer = "Page1.xaml") {

'do something

}

else {

'do something else

}

How do I get the 'referer' above

Thanks!

Roberto Sonnino

Microsoft Student Ambassador

http://www.virtualdreams.com.br




Answer this question

How to get previous (referer) scene/page?

  • SWMagic

    Roberto,

    Ahh, yes my mistake. I haven't done much hands on work with NavigationService myself, I just knew it existed and was hoping to get you pointed in the right direction. I wasn't aware there was another NavigationService concept in another namespace when I was searching, so it threw me off a little.

    I find it interesting that you can add and remove journal entries, but can't enumerate them in any way. Strange. Guess I'll keep my eyes on this thread for the answer too.

    Good luck,
    Drew


  • Jonty

    One way is to twiddle your application's Application...

    public partial class MyApp : Application

    {

    private static Uri referrer;

    public static Uri Referrer

    {

    get { return referrer; }

    }

    protected override void OnNavigating(NavigatingCancelEventArgs e)

    {

    base.OnNavigating(e);

    referrer = null;

    object navigator = e.Navigator;

    if (navigator is NavigationWindow)

    {

    // NavigationWindow and RootBrowserWindow

    referrer = ((NavigationWindow)navigator).CurrentSource;

    }

    else if (navigator is Frame)

    {

    referrer = ((Frame)navigator).CurrentSource;

    }

    }

    protected override void OnNavigationStopped(NavigationEventArgs e)

    {

    base.OnNavigationStopped(e);

    referrer = null;

    }

    }

    ...

    public partial class Page2 : Page

    {

    public Page2()

    {

    InitializeComponent();

    this.WindowTitle = MyApp.Referrer.OriginalString;

    }

    }


  • Peri3

    Drew,

    Sorry, but I think you made a mistake. You pointed me to a History property from another NavigationService (from the Microsoft.Web.Management.Client namespace, not from the System.Windows.Navigation namespace). This NavigationService has nothing to do with WPF page nav, and the System.Windows.Navigation.NavigationService does not have a History property. The only thing closer to what I want would be the Journal, which I had studied and which does not have any accessible way of detecting the previous page.

    Therefore, this doesn't solve my problem, but thanks for your help anyway. Any help is appreciated.

    Thanks!

    Roberto Sonnino

    Microsoft Student Ambassador

    http://www.virtualdreams.com.br



  • SamusAran03

    You can get access to details about pages that came before yours using NavigationService's History property. Here's an overview of Navigation. You can get access to the NavigationSerivce through the Page's NavigationService property.

    HTH,
    Drew


  • teTTix

    Whoa! I was thinking about the same solution (use a property in the MainApplication/MyApp class) when you posted it. Thanks! It works!

    Roberto



  • How to get previous (referer) scene/page?