Frage

If I have to, I will pass an arg to a page when I navigate to it, but is it necessary - is there a way to know which page called Navigate() without doing this?

War es hilfreich?

Lösung 2

You could use GetNavigationState but it has some undesirable side-effects (it navigates away from the current page and the string is internal with no guarantees about how it might/might not change). I think you're best off passing it.

Calling this method will call Page.OnNavigatedFrom for the current page using NavigationMode.Forward. GetNavigationState is usually called when the application is being suspended, so the current page is navigated away from.

Note:

The serialization format used by these methods is for internal use only. Your app should not form any dependencies on it. Additionally, this format supports serialization only for basic types like string, char, numeric and GUID types.

Andere Tipps

Keep a static property in the Application object called PreviousPage. In the OnNavigatedFrom event of every page (or in the base class) set the PreviousPage to "this" (current page). In the OnNavigatedTo event, you can check that Application.PreviousPage property and use it how you see fit. This is the most effective way of accessing the previous page as there is no Frame.BackStack property in the framework for Windows Store Applications.

I ran into this exact problem and Caleb's answer was very helpful. I just wanted to post some code to clarify how I used his solution.

DISCLAIMER I'm a bit (very!) new to C#, I'm sure there's a better way to do this, but I wanted to provide it as a starting point in case somebody else was looking for a bit more detail.

I created a global variable in the application's namespace to hold the previous page state:

public static class global_vals{
    static object _previousPage;
    public static object previousPage
    {
        get
        {
            return _previousPage;
        }
        set
        {
            _previousPage = value;
        }
    }
}

The OnNavigatedFrom method on each of my pages looks something like this:

protected override void OnNavigatedFrom(NavigationEventArgs e)
{
    base.OnNavigatedFrom(e);
    global_vals.previousPage = this;
}

And the OnNavigatedTo method checks the global variable like this:

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    base.OnNavigatedTo(e);
    if (global_vals.previousPage.GetType().ToString() == "AppName.SomePage")
    {
        //do something
    }
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top