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
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();
}
}
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.
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
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