如何知道哪个是我在WindowsPhone8.1中的最后一页
本文关键字:最后 一页 WindowsPhone8 何知道 | 更新日期: 2023-09-27 18:21:14
所以在WP7和WP8中,我在第2页这样做是为了知道我是否来自第1页:
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"))
{
}
}
在WP8.1中要做什么?
在Windows Phone 8.1中,您可以使用Frame(当前页面)的BackStack属性。
使用以下代码,您将获得导航到新页面的页面:
var lastPage = Frame.BackStack.Last().SourcePageType
在WP8.1运行时中,您有一个用于导航的Frame类,在那里您还可以找到BackStack属性,您可以从中读取以前的页面。
示例如下:
/// <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();
}
}
在OnNavigatedTo函数中,您可以使用以下代码来确定上次访问的页面
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}
}
}
适用于wp 8.1 silverlight
var previousPage = NavigationService.BackStack.FirstOrDefault().Source;
if (previousPage!=null && previousPage.ToString().Contains("MainPage"))
{
// do your work
}