Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know which was my last page in Windows Phone 8.1

So in WP7 and WP8 I did this in Page2 to know if I had came from Page1:

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

        var lastPage = NavigationService.BackStack.FirstOrDefault();

        if (null != lastPage && true == lastPage.Source.ToString().Contains("Page1.xaml"))
        {

        }
    }

What to do in WP8.1?

like image 579
v.g. Avatar asked Dec 07 '22 00:12

v.g.


2 Answers

In Windows Phone 8.1, you can use the BackStack property of the Frame (current page).

Using the following code you will get the page which originated the navigation to the new page:

var lastPage = Frame.BackStack.Last().SourcePageType
like image 85
meneses.pt Avatar answered Dec 08 '22 12:12

meneses.pt


In WP8.1 RunTime you have a Frame class which you use to navigate, there you will also find BackStack property, from which you can read previous pages.

A sample example can look like this:

/// <summary>
/// Method checking type of the last page on the BackStack
/// </summary>
/// <param name="desiredPage">desired page type</param>
/// <returns>true if last page is of desired type, otherwise false</returns>
private bool CheckLastPage(Type desiredPage)
{
    var lastPage = Frame.BackStack.LastOrDefault();
    return (lastPage != null && lastPage.SourcePageType.Equals(desiredPage)) ? true : false;
}

protected override async void OnNavigatedTo(NavigationEventArgs e)
{
    base.OnNavigatedTo(e);
    if (CheckLastPage(typeof(MainPage)))
    {
        // do your job
        await new MessageDialog("Previous is MainPage").ShowAsync();
    }
}
like image 25
Romasz Avatar answered Dec 08 '22 12:12

Romasz