本文介绍了在windows phone 8.1中识别从哪个页面来到这个页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

如何知道我在 Windows 手机 8.1 中来自哪个页面?例如,我有 3 个页面:登录页面、主页和类别页面.我想要做的是识别用户是从哪些页面进入主页的;从登录页面或类别页面.

How can I know which page I came from in a Windows phone 8.1? For example I have 3 pages: a Login page, a main page and a category page. What I want to do is to identify from which of those pages a user came to the main page; from the login page or from the category page.

我在 MainPage.cs 中的 OnNavigatedTo 方法中尝试了这段代码,

I tried this code inside the OnNavigatedTo method in MainPage.cs,

var lastPage = this.Frame.BackStack.Last();

if (lastPage != null && lastPage.SourcePageType.Name.ToString() == "LogInPage")
{
}
else
{
}

但问题是在我转到类别页面并返回主页面后,它仍然提供一个登录页面作为最后一个堆栈.如何解决?

But the thing is after I go to category page and return back to main page still it gives a login page as last stack. How to resolve that?

推荐答案

没错.当您通过硬件后退按钮从类别页面导航回主页时,后退堆栈上的最后一个条目从后退堆栈弹出并推到前向堆栈的末尾.

That's correct. When you navigate from the category page back to the main page by means of the hardware back button, the last entry on the back stack is popped off the back stack and pushed onto the end of the forward stack.

因此,您可以通过两种不同的方式导航到页面MainPage:

So, there's two different ways in which you can navigate to a page MainPage:

  1. 您可以通过Frame.Navigate(typeof(MainPage))Frame.GoForward()导航前进,如果MainPage 位于 Frame.ForwardStack 的最后.您所在的上一页是 Frame.BackStack.Last().
  2. 如果MainPageFrame.BackStack中排在最后,您可以通过Frame.GoBack()向后导航>.您所在的上一页是 Frame.ForwardStack.Last().
  1. You can navigate forward by means of Frame.Navigate(typeof(MainPage)), or Frame.GoForward() if MainPage is last in Frame.ForwardStack. The previous page that you were on is Frame.BackStack.Last().
  2. You can navigate backward by means of Frame.GoBack() if MainPage is last in Frame.BackStack. The previous page that you were on is Frame.ForwardStack.Last().

那么,你怎么知道是查询后栈还是前栈呢?在您的 Page.OnNavigatedTo() 方法覆盖中,检查 e.NavigationMode 的值.如果其值为 NavigationMode.NewNavigationMode.Forward,则检查返回堆栈.如果其值为 NavigationMode.Back,则检查前向堆栈.

So, how do you know whether to consult the back stack or the forward stack? In your Page.OnNavigatedTo() method override, check the value of e.NavigationMode. If its value is NavigationMode.New or NavigationMode.Forward, then check the back stack. If its value is NavigationMode.Back, check the forward stack.

这是一些代码:

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    Type lastPage = null;

    switch (e.NavigationMode)
    {
        case NavigationMode.New:
        case NavigationMode.Forward:
            if (Frame.BackStack.Count > 0)
                lastPage = Frame.BackStack.Last().SourcePageType;
            break;

        case NavigationMode.Back:
            if (Frame.ForwardStack.Count > 0)
                lastPage = Frame.ForwardStack.Last().SourcePageType;
            break;

        default:
            // TODO
            break;
    }

    if (lastPage != null)
        System.Diagnostics.Debug.WriteLine("Last page was {0}", lastPage);
}

这篇关于在windows phone 8.1中识别从哪个页面来到这个页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-07 01:18