【问题标题】:Back button navigation closes my app on Windows 10 Mobile后退按钮导航关闭我在 Windows 10 移动版上的应用
【发布时间】:2016-03-08 23:13:37
【问题描述】:

我有一个应用程序,它在 Windows 10 上按预期运行。但在 Windows 10 移动版上,当用户按下后退按钮时,应用程序关闭。在我返回请求的事件处理程序中,我什至注释掉了 GoBack() 方法,但应用程序仍然关闭。

private void MobileNavigationService_BackRequested(object sender, BackRequestedEventArgs e)
    {
        e.Handled = true;

        var navigationService = UnityConfiguration.Resolve<IMobileNavigationService>();

        if (navigationService.CanGoBack())
        {
            //navigationService.GoBack();
        }
    }

我有一种感觉,即使我设置了 e.Handled = true,它也会忽略它并表现得像没有处理程序一样。

更新

作为附加信息

App 只有一页,Shell。它有常见的东西,比如菜单和标题栏。它也有框架。在框架中,我打开所有其他页面。因此,对于我的应用程序而言,返回意味着返回该框架,而不是整个应用程序。我想覆盖默认行为。

【问题讨论】:

  • 这是默认行为。你期待什么?
  • 是的,但是当 e.Hnalded 设置为 true 时,必须覆盖默认行为。 see here
  • 我相信应用程序实际上并没有关闭 - 它只是切换到开始屏幕,但应用程序在后台继续运行。这是用户期望发生的事情。
  • 这不是我所期望的。其实我会把它导航回上一页。
  • IMobileNavigationService的实现是什么?我认为这一定是您未显示的代码中的某些内容。这个示例 Prism/Unity 应用程序没有特殊代码:github.com/PrismLibrary/Prism-Samples-Windows/tree/master/…

标签: c# win-universal-app windows-10-mobile


【解决方案1】:
  • 为 BackRequested 设置事件处理程序并在页面堆栈中导航回来。
  • 根据应用的内部页面堆栈启用和禁用标题栏后退按钮。

您可以在 GitHub 上从 Microsoft 获取此 DEMO

Back Button Sample

protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        bool optedIn = false;

        if ((bool)e.Parameter)
        {
            optedIn = true;

        }

        Frame rootFrame = Window.Current.Content as Frame;

        if (rootFrame.CanGoBack && optedIn)
        {
            // If we have pages in our in-app backstack and have opted in to showing back, do so
            SystemNavigationManager.GetForCurrentView().AppViewBackButtonVisibility = AppViewBackButtonVisibility.Visible;
        }
        else
        {
            // Remove the UI from the title bar if there are no pages in our in-app back stack
            SystemNavigationManager.GetForCurrentView().AppViewBackButtonVisibility = AppViewBackButtonVisibility.Collapsed;
        }
    }

【讨论】:

  • 更新源代码
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-04-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-02
  • 2020-09-20
相关资源
最近更新 更多