Can I find out which page a user came from on Back Button press on WP7?
I haven't had much luck finding the answer through google sea开发者_如何学编程rches, 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.
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();
}
}
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
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.
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?
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
精彩评论