I know that in WP7 it is not possible to exit application programatically. So haw can I handle the following need? My MainPage is empty, and has has the only purpose to make a test: if user never filled a preference page, redirects to Page_B.xaml (a page which collects his preferences, such as language aond other info which are needed in order to run the app). Otherwise redirect to Page_A.xaml. So the first page that user is shown is either Page_A or Page_B (depending if this is the first time he/she runs the app).
HERE IS THE PROBLEM: when user select the hardware "Back" button while in Page_A or Page_B, I want to quit application. Instead he is redirected to the mainPage, which shows nothing. So I need to exit applicatin when user selects "Back" in Page_A or Page_B (OnBackKeyPress()) , or more generally when user comes to MainPage.xaml using the Back button. Is there a way to exit application without showing the empty MainPage.xaml? Thanks for your advice. Emilio
here is the simplified code in MainPage.xaml:
public MainPage(){
InitializeComponent();
if (phoneAppService.State.TryGetValue("currentLanguage", out someObject))
{ // Yes: go on
var uri = "/Pages/Page_A.xaml";
this.Dispatcher.BeginInvoke(() => this.NavigationService.Navigate(new Uri(uri, UriKind.Relative)));
}
else
{ // No: select language before proceeding
var uri = "/Pages/Page_B.xaml";
this.Dispatcher.BeginInvoke( () => this.NavigationService.Navigate(new Uri(uri, UriKind.Relative)));
}
}
**// if previous page was Page_A or Page_B then exit application**
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
string sourcePage = "";
if (NavigationContext.QueryString.TryGetValue("from", out sourcePage)) {
if ((string.Compare(sourcePage.ToString(), "Page_A")) == 0 ? true : false) {
**// EXIT APPLICATION**
}
if ((string.Compare(sourcePage.ToString(), "Page_B")) == 0 ? true : false) {
**// EXIT APPLICATION**
}
}
base.OnNavigatedTo(e);
}
Page_A.xaml has the following code to send information to MainPage.
// Back Button pressed: notify MainPage so it can exit application
protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
{
NavigationService.Navigate(new Uri(uri, UriKind.Relative));
base.OnBackKeyPress(e);
}
Page_B.xaml has the following code to send information to MainPage.
// Back Button pressed: notify MainPage so it can exit application
protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
{
var uri = "/MainPage.xaml?from=Page_B";
NavigationService.Navigate(new Uri(uri, UriKind.Relative));
base.OnBackKeyPress(e);
}