加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Windows > 正文

windows-phone-7 – 在WP7上的“后退”按钮上退出应用程序

发布时间:2020-12-14 04:19:41 所属栏目:Windows 来源:网络整理
导读:我知道在WP7中无法以编程方式退出应用程序.那么我可以处理以下需求吗? 我的MainPage是空的,并且唯一的目的是进行测试: 如果用户从未填写首选项页面,则重定向到Page_B.xaml(收集其偏好的页面,例如语言以及运行应用程序所需的其他信息).否则重定向到Page_A.x
我知道在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);
    }

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读