Windows phone 8.1 BackPressed 无法正常工作 [英] Windows phone 8.1 BackPressed not working properly

查看:24
本文介绍了Windows phone 8.1 BackPressed 无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Windows Phone 8.1 新世界.基本功能是点击后退按钮.那个功能不能正常工作是这个windows phone 8.1.是这种行为还是我弄错了.

Windows phone 8.1 new to world. Basic function is back button click. Is that function not working properly is this windows phone 8.1. Is that behavior or i'm made mistake.

以下代码在主页中使用,但此代码在单击返回时也从所有其他类调用.我只需要在主页上访问以下方法.

Below code using in Homepage but this code calling from all other class too while clicking back. I need to access below method only on Home page .

请检查下面的代码并向我推荐好的解决方案.

Please check below code and refer me good solution.

请看我的代码:

 public HomePage()
 {
  this.InitializeComponent(); 
  Windows.Phone.UI.Input.HardwareButtons.BackPressed += HardwareButtons_BackPressed;
 }

    void HardwareButtons_BackPressed(object sender, BackPressedEventArgs e)
    {

    }

谢谢

推荐答案

它工作正常.BackPressed 事件在应用范围内有效.我想到的两个选项:

It is working properly. The BackPressed event is working app-wide. Two options that come to my mind:

  • 编写能够识别您当前调用它的 Page 的事件处理程序 - 简单示例如下所示:

  • write eventhandler that would recognize the Page in which you currently invoke it - simple example can look like this:

private void HardwareButtons_BackPressed(object sender, Windows.Phone.UI.Input.BackPressedEventArgs e)
{
    Frame frame = Window.Current.Content as Frame;
    if (frame == null) return;

    if (frame.Content is HomePage)
    {
        e.Handled = true;
        Debug.WriteLine("I'm in HomePage");
    }
    else if (frame.CanGoBack)
    {
        frame.GoBack();
        e.Handled = true;
    }
}

  • 第二个选项 - 进入 Page 时订阅 Windows.Phone.UI.Input.HardwareButtons.BackPressed,离开 时取消订阅页.请注意,这种方式存在一些缺陷 - 您必须正确处理 OnNavigatedTo、OnNavigatedFrom、Suspending 和 Resuming(更多关于 生命周期在这里).另请注意,订阅应在其他人之前完成 - 例如 NavigationHelper.

  • second option - subscribe to Windows.Phone.UI.Input.HardwareButtons.BackPressed when you enter the Page and unsubscribe when you leave the Page. Note that in this way there are some pitfalls - you have to handle properly OnNavigatedTo, OnNavigatedFrom, Suspending and Resuming (more about Lifecycle here). Note also that the subscription should be done before others - for example NavigationHelper.

    一些说明 - 上面的代码应该可以工作,但也取决于其他情况:

    Some remarks - the above code should work, but it also depends on other circumstances:

    • 如果之前(在 App.xaml.cs 中)有其他内容订阅了 BackPressed - 请记住,通常事件是按订阅顺序触发的
    • 检查您是否正在使用 NavigationHelper - 它也订阅了 BackPressed
    • 切记不要多次订阅
    • 记得允许用户离开您的主页
    • if there is something other subscribed to BackPressed before (in App.xaml.cs) - remember that usually events are fired in order they were subscribed
    • check if you are using NavigationHelper - it also subscribes to BackPressed
    • remember not to subscribe multiple times
    • remember to allow the User to leave your HomePage

    这篇关于Windows phone 8.1 BackPressed 无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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