Can I find out which page a user came from on Back Button press on WP7?

亡梦爱人 提交于 2019-12-30 06:52:27

问题


I haven't had much luck finding the answer through google searches, but is it possible to tell which page a user came from?

Or, send a query string on back button press, so I can tell?

Basically, I have a page that I don't want a user to get to by pressing the back button -- the only way they should get there is if they followed the process from the start. A good example of what I'm trying to do is not allow a user to go back to the confirmation setup while registering for an account after they have already successfully registered. I'd rather them go to the start of registration.


回答1:


You can use the NavigationService.BackStack property and check the first entry in the back stack in the page as it is navigated to.

If this check needs to be done in several pages, it could be put into a base class. Also, if your situation fits the eula/login scenario mentioned by @Skomski, his answer makes the most sense.

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

    var lastPage = NavigationService.BackStack.FirstOrDefault();

    if (lastPage != null && lastPage.Source.ToString() == "/MainPage.xaml")
    {
        NavigationService.RemoveBackEntry();
    }
}



回答2:


Basically you want to remove your NavigationEntrys.

For this, use NavigationService.RemoveBackEntry.

You can access the NavigationService from anywhere, using this snippet:

(App.Current.RootVisual as PhoneApplicationFrame).RemoveBackEntry()

Better solution:

Regarding EULA / Login screens (and Splash) - don't make them into pages. If you instead make them Popup or Dialog controls you can show or hide them at any time (on first navigation; when the user hits a "protected" part of the app; after a time-out; etc.) and they don't consume a slot in the backstack.

Source: http://blogs.msdn.com/b/ptorr/archive/2010/08/01/exiting-a-windows-phone-application.aspx




回答3:


One method is to override OnBackKeyPress on every page (except the main page, because that would be pointless). Within that function assign the name of the current page to a global variable. Now the page you've navigated back to can inspect the global variable and determine how you got there.




回答4:


I can't think of a way of detecting a backbutton press directly.

How about a session variable that records the current stage in the process?




回答5:


You can use the NavigationService to accomplish the desired result. Specifically, you can do something like this:

var uri = NavigationService.CurrentSource;
if(uri != badUri) { /*proceed...*/ } 

Here is the reference page:

http://msdn.microsoft.com/en-us/library/system.windows.navigation.navigationservice.currentsource.aspx



来源:https://stackoverflow.com/questions/6999028/can-i-find-out-which-page-a-user-came-from-on-back-button-press-on-wp7

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!