Exit the application to the back button on WP7

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.

 // 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); } 
+6
source share
5 answers

This is a fairly common scenario when you perform a one-time task the first time you launch the application, or if you need to log in to use the application in general. Instead of writing this as a full page, I would recommend placing UserControl in a popup popup on the main page. Thus, a single press of the back key will always exit your application.

+11
source

You can achieve this goal, for example. having a static boolean in the App class, for example. ForceExitApplication and set this to true on page_A or Page_B. On MainPage, you check this variable, and if its value is true, you will leave the application:

  • Or by calling NavigationService.GoBack () (I think it works)
  • Or, throwing an exception (this will work, but definitely a failure on the market)

I would, however, be different from implementing this behavior. It seems that what you are trying to achieve - exiting the application at a point other than the main page - is against the recommendations of WP7, and if so, your application will most likely be rejected when submitted until you fix this problem.

+2
source

You might want to restructure your application. You donโ€™t have MainPage at all, always load the page. If the user has not set preferences, just redirect them to PageB, they will set the settings and click the "Back" button, which will return them to the PageA page. Since the application now has the settings it needs, it can display the page normally.

If you really have to use a 3-page scheme, you can get a NonLinear Navigation Service to work your magic.

+2
source

In Mango, you can use the following approach:

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

  RootFrame.Navigated += RootFrame_Navigated; 

Then in the event handler we can use the navigation back:

 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 this code does: First, we check if we went to page A or B. Then we check to see if we have any pages in backstack mode. (This should be at least one, because we have already been redirected from mainPage). Then we delete the last entry from the backstack log. Now the user clicks the back button, the application will exit.

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

+2
source

The root element in the visualization tree of most pages is the grid. Itโ€™s trivial to organize your two user interfaces into two grids placed on the stack and assign each visibility to the pageโ€™s dependency property so that this property controls visibility. There is no reason that one page cannot serve both purposes.

0
source

Source: https://habr.com/ru/post/893880/


All Articles