I know that in WP7 it is not possible to exit the application programmatically. So can I handle this need? My MainPage is empty and has the sole purpose of making a test: if the user has never filled out the preferences page, it redirects to the Page_B.xaml page (the page that collects its settings, such as the language and other information necessary to launch the application). Otherwise, redirect to Page_A.xaml. Thus, the first page on which the user is displayed is either Page_A or Page_B (depending on when he launches the application for the first time).
HERE IS THE PROBLEM: when the user selects the hardware Back button while in Page_A or Page_B, I want to exit the application. Instead, it redirects to the main page, which shows nothing. Therefore, I need to exit the application when the user selects "Back" in Page_A or Page_B (OnBackKeyPress ()), or, more often, when the user comes to MainPage.xaml using the "Back" button. Is there a way to exit the application without showing an empty MainPage.xaml? Thank you 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.
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); }
source share