【发布时间】:2011-07-29 16:02:46
【问题描述】:
我知道在 WP7 中不可能以编程方式退出应用程序。那么我该如何处理以下需求? 我的 MainPage 是空的,唯一的目的是进行测试: 如果用户从未填写首选项页面,则重定向到 Page_B.xaml(一个收集他的首选项的页面,例如语言和运行应用程序所需的其他信息)。否则重定向到 Page_A.xaml。 因此,用户显示的第一页是 Page_A 或 Page_B(取决于这是他/她第一次运行应用程序)。
这是问题所在: 当用户在 Page_A 或 Page_B 中选择硬件“返回”按钮时,我想退出应用程序。相反,他被重定向到 mainPage,它什么也没显示。 因此,当用户在 Page_A 或 Page_B (OnBackKeyPress()) 中选择“返回”时,或者更一般地,当用户使用“后退”按钮进入 MainPage.xaml 时,我需要退出应用程序。 有没有办法在不显示空 MainPage.xaml 的情况下退出应用程序? 谢谢你的建议。 埃米利奥
这是 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。
// 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。
// 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);
}
【问题讨论】:
标签: windows-phone-7 button exit back