Reputation: 1076
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?
Upvotes: 4
Views: 4252
Reputation: 6178
It works for wp 8.1 silverlight
var previousPage = NavigationService.BackStack.FirstOrDefault().Source;
if (previousPage!=null && previousPage.ToString().Contains("MainPage"))
{
// do your work
}
Upvotes: 0
Reputation: 29792
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();
}
}
Upvotes: 5
Reputation: 4292
In your OnNavigatedTo function you can use following code to determin your last visited page
protected override void OnNavigatedTo(NavigationEventArgs e)
{
if(this.Frame.BackStack.Count>0)
{
var lastPage = this.Frame.BackStack[this.Frame.BackStackDepth - 1];
string lastPageName = lastPage.SourcePageType.Name;
if(lastPageName == "MainPage")
{//This is main page}
}
}
Upvotes: 2
Reputation: 2616
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
Upvotes: 8