Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exit application on "Back" button on WP7

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);
        }
like image 450
Emilio Avatar asked Jul 29 '11 16:07

Emilio


2 Answers

This is a fairly common scenario with either doing a one-off task on 1st time run of an app, or if you need to login to use the app at all. Rather than writing this as a full page I'd recommend putting a UserControl in a full-screen Popup over your main page. That way a single Back key press will always exit your app.

like image 189
Paul Annetts Avatar answered Sep 28 '22 08:09

Paul Annetts


You may want to restructure your application. Do not have a MainPage at all, always load PageA. If the user hasn't set preferences, just redirect them to PageB, they'll set preferences and hit the Back button which takes them back to PageA. Since the app now has the settings it needs it can display PageA normally.

If you really must use the 3 page scheme, you may be able to get the NonLinear Navigation Service to work its magic.

like image 29
Praetorian Avatar answered Sep 28 '22 08:09

Praetorian