“返回"退出应用程序WP7 上的按钮 [英] Exit application on "Back" button on WP7

查看:12
本文介绍了“返回"退出应用程序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 时,我需要退出应用程序.有没有办法退出应用程序而不显示空的 MainPage.xaml?谢谢你的建议.埃米利奥

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 放在主页上的全屏弹出窗口中,而不是将其写成整页.这样,按一次返回键将始终退出您的应用.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆