开发者

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


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.


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.


You could achieve this goal by e.g. having a static boolean variable on the App class, e.g. ForceExitApplication and set this to true on Page_A or Page_B. On MainPage you would check this variable and if its set to true you exit the application:

  • Either by calling NavigationService.GoBack() (I think this would work)
  • Or by throwing an exception (this would work, but will definitely fail marketplace submission)

I would, however be vary of implementing this behaviour. It seems that what you are trying to achieve - exiting the application at a point other than the main page - is against the WP7 guidelines and if this is so, your application is likely to get rejected when submitting until you fix this issue.


In Mango you could use the following approach:

in app.cs add event handler to RootFrame.Navigated event:

 RootFrame.Navigated += RootFrame_Navigated;

Then within the event handler, we could utilize the navigation backstack:

void RootFrame_Navigated(object sender, NavigationEventArgs e)
{
    var pageBURI = "/Pages/Page_B.xaml";
    var pageAURI = "/Pages/Page_A.xaml";

    if ((e.Uri == pageAURI || e.Uri == pageBURI) && RootFrame.BackStack.Count() > 0)
    {
        RootFrame.RemoveBackEntry();
    }
}

What does this code do: First, we are checking, if we have navigated to page A or B. Then we are checking, if we have any pages in navigation backstack. (It should be at least one, because, we had already been redirected from mainPage). Then, we are removing the last entry from the backstack journal. Now, of a user will press the "Back" button, the application will exit.

You could read more about BackStack here: http://msdn.microsoft.com/en-us/library/hh394012(v=vs.92).aspx


The root item in the visual tree of most pages is a grid. It is trivial to organise your two UIs into two grids hosted by a stackpanel and bind the visibility of each to a page dependency property so that this property controls which is visible. There is no reason a single page can't serve both purposes.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜