在WP7上的"后退"按钮上退出应用程序

Emi*_*lio 6 button exit back windows-phone-7

我知道在WP7中无法以编程方式退出应用程序.那么我可以处理以下需求吗?我的MainPage是空的,并且唯一的目的是进行测试:如果用户从未填写首选项页面,则重定向到Page_B.xaml(收集他的首选项的页面,例如语言以及运行所需的其他信息)该应用程序).否则重定向到Page_A.xaml.因此,显示用户的第一页是Page_A或Page_B(取决于这是否是他/她第一次运行应用程序).

这就是问题:当用户在Page_A或Page_B中选择硬件"后退"按钮时,我想退出应用程序.相反,他被重定向到主页,它没有显示任何内容.因此,当用户在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);
    }
Run Code Online (Sandbox Code Playgroud)

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);
}
Run Code Online (Sandbox Code Playgroud)

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);
        }
Run Code Online (Sandbox Code Playgroud)

Pau*_*tts 11

这是一个相当常见的情况,要么在第一次运行应用程序时执行一次性任务,要么根据需要登录才能使用该应用程序.而不是将其写为整页我建议将UserControl放在主页上的全屏弹出窗口中.这样,单个Back键按下将始终退出您的应用程序.


Ger*_*osz 2

您可以通过在 App 类上使用静态布尔变量(例如 ForceExitApplication)并在 Page_A 或 Page_B 上将其设置为 true 来实现此目标。在 MainPage 上,您将检查此变量,如果其设置为 true,则退出应用程序:

  • 通过调用 NavigationService.GoBack() (我认为这可行)
  • 或者通过抛出异常(这可以工作,但肯定会导致市场提交失败)

然而,我会采取不同的方式来实现这种行为。看来您想要实现的目标 - 在主页以外的位置退出应用程序 - 违反了 WP7 准则,如果是这样,您的应用程序在提交时可能会被拒绝,直到您解决此问题。