c# - Windows Phone 8.1 中的 BackButtonPressed 问题?

标签 c# windows-phone-8.1 win-universal-app

我的 Windows Phone 8.1 运行时应用程序中有一个弹出窗口。

当按下后退按钮并在页面中打开弹出窗口时,应用程序应该留在页面本身,否则它应该返回。这是我的概念。所以,我编码如下:

    void HardwareButtons_BackPressed(object sender, BackPressedEventArgs e)
    {
        if (PopupWindow.IsOpen)
        {
            PopupWindow.IsOpen = false;
            e.Handled = true;
        }
    }

即使弹出窗口在页面中打开,应用程序也会转到上一页。我在 Windows Phone silverlight 应用程序中使用了相同的逻辑并且有效。

注意:我使用的是基本页面。

我到底犯了什么错误?

最佳答案

检查两件事:

  • NavigationHelper 默认情况下,HardwareButtons_BackPressed 缺少检查事件是否已被处理,尝试改进它:

    private void HardwareButtons_BackPressed(object sender, Windows.Phone.UI.Input.BackPressedEventArgs e)
    {
        // if (this.GoBackCommand.CanExecute(null)) // this is as a default
        if (this.GoBackCommand.CanExecute(null) && !e.Handled) //  add a check-up
        // ... rest of the code
    
  • 查看您的 App.xaml.cs 文件,在 App() 中订阅了 HardwareButtons_BackPressed(检查订阅的方法是否也向后导航):

    public App()
    {
        this.InitializeComponent();
        this.Suspending += OnSuspending;
        //  HardwareButtons.BackPressed += HardwareButtons_BackPressed; // this line also could fire Frame.GoBack() (as default project template)
        // of course check what is in the above method
    }
    

另请记住,事件是按照您订阅它们的顺序触发的,例如导航助手在 Loaded 事件中订阅。如果您在之后订阅,那么导航将是第一位的。您可以先订阅,也可以使用标志。

关于c# - Windows Phone 8.1 中的 BackButtonPressed 问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24081721/

相关文章:

c# - UWP 漫游应用程序数据的大小是文件实际大小还是磁盘上的大小?

c# - Azure Functions - 无法从 Azure WebJobs SDK 调用

c# - 订单和订单详情

c# - MVVM 调度程序 UI 无响应

c# - Windows Phone 8.1 Assembly.GetExecutingAssembly 不可用

xaml - 在 UWP 上禁用/更改枢轴动画

c# - 如何确保跨多个枚举的唯一性?

windows-8.1 - Windows Phone 通用应用程序中的电子邮件发送任务

c# - Windows Phone 8.1 中 MapControl Center 属性的数据绑定(bind)

c# - 如何添加带有隐藏代码的新 XAML View