在“返回"上退出应用程序. WP7上的按钮 [英] Exit application on "Back" button on WP7
问题描述
我知道在WP7中不可能以编程方式退出应用程序.那么我该如何满足以下需求? 我的MainPage是空的,并且具有进行测试的唯一目的: 如果用户从未填写过首选项页面,则重定向到Page_B.xaml(收集其首选项的页面,例如语言以及运行该应用所需的其他信息).否则,重定向到Page_A.xaml. 因此,显示给用户的第一页是Page_A或Page_B(取决于这是他/她是第一次运行该应用程序).
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).
这里是问题: 当用户在Page_A或Page_B中选择硬件返回"按钮时,我要退出应用程序.相反,他被重定向到显示任何内容的mainPage. 因此,当用户在Page_A或Page_B(OnBackKeyPress())中选择返回"时,或更一般而言,当用户使用返回"按钮进入MainPage.xaml时,我需要退出applicatin. 有没有一种方法可以在不显示空MainPage.xaml的情况下退出应用程序? 谢谢你的建议. 埃米利奥(Emilio)
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
这是MainPage.xaml中的简化代码:
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具有以下代码,可将信息发送到MainPage.
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具有以下代码,可将信息发送到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);
}
推荐答案
这是一个相当普遍的场景,要么在应用程序第一次运行时执行一次一次性任务,要么需要登录才能在以下位置使用该应用程序全部.建议您将UserControl放置在主页上的全屏弹出窗口中,而不是将其写为完整页面.这样一来,只要按一下Back键,总会退出您的应用程序.
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.
这篇关于在“返回"上退出应用程序. WP7上的按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!