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?
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
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();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With