【问题标题】:Suspending UWP apps using back button使用后退按钮暂停 UWP 应用
【发布时间】:2017-01-17 06:16:49
【问题描述】:

有没有一种方法可以通过按下后退按钮来“暂停”UWP 应用程序。 我什至不确定暂停是正确的术语,但我想做的是在用户按下后退按钮时关闭应用程序,而不是返回到我的应用程序中的注册/登录页面。 我希望应用程序关闭,但当用户通过按住后退按钮打开最近的应用程序菜单时仍会显示该应用程序。 这是我在 App.Xaml.Cs 中的代码

 private void OnBackRequested(object sender, Windows.UI.Core.BackRequestedEventArgs e)
        {
            Frame rootFrame = Window.Current.Content as Frame;
            if (rootFrame.CurrentSourcePageType == typeof(Pages.Home))
            {
                App.Current.Exit();
            }
            if (rootFrame.CanGoBack)
            {
                e.Handled = true;
                rootFrame.GoBack();
            }
        }

问题出在 App.Current.Exit(); 它终止了我想要做的只是关闭而不返回主页面的应用程序。 我该怎么做?

【问题讨论】:

    标签: c# xaml uwp


    【解决方案1】:

    要离开您的应用而不终止它,只需删除 App.Current.Exit(); 并忽略返回事件 (e.Handled = false;)。然后,您将获得默认行为,即将应用程序留在移动设备上。在桌面上,您的应用程序将保留在原来的页面上。然后,您必须执行任何满足您需要的操作,以使用暂停/恢复事件处理程序将您的应用保存/恢复到适当的状态。

    void OnBackRequested(object sender, Windows.UI.Core.BackRequestedEventArgs e)
    {
            Frame rootFrame = Window.Current.Content as Frame;
            if (rootFrame.CurrentSourcePageType == typeof(Pages.Home))
            {
                // ignore the event. We want the default system behavior
                e.Handled = false;
            }
            else if (rootFrame.CanGoBack)
            {
                e.Handled = true;
                rootFrame.GoBack();
            }
    }
    

    【讨论】:

    • 这让我回到了登录/注册页面,我希望应用程序在该页面上表现得像没有剩余页面一样,当我在主页上时自行终止
    • 我忘记了“else”子句;我已经更新了我的答案。使用您的代码,始终评估 rootFrame.CanGoBack 并始终返回“真”,因为您在历史记录中有一个页面,因此始终执行后退导航。您还可以通过清除 rootFrame.BackStack 从历史记录中删除登录/注册页面。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-20
    • 2017-12-13
    • 2018-04-30
    • 1970-01-01
    • 2016-05-06
    • 1970-01-01
    相关资源
    最近更新 更多