在Windows Phone 8.1 RT App中导航时出现后退按钮问题 [英] Issue with back button when navigating in Windows Phone 8.1 RT App

查看:112
本文介绍了在Windows Phone 8.1 RT App中导航时出现后退按钮问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚使用以前专门使用Silverlight for Windows Phone应用程序的RT API为Windows Phone 8.1制作应用程序。



我正在创建一个基本的不同页面的应用程序。主页面只是一个菜单页面,其中包含将用户带到不同页面的按钮列表。我遇到的问题是让用户从其他页面回到主页面。



我有两种类型的子页面。


第一个只是普通页面,当用户点击后退按钮时,它们会被带回主页面。

但是我有一些带有枢轴控制的页面。当用户点击后退按钮时,他们将被带入之前的应用程序。 (我的应用程序仍然在后台运行)。



有人可以解释为什么只有带有枢轴控件的页面才能让用户在我们的应用程序出现时后退按钮而不是上一页(在本例中是主页)?



任何帮助都非常感谢,我已经做了一些搜索而且我是意识到导航模型已在Windows Phone 8中更改,然后在Windows Phone 8.1 RT应用程序中更改,以使它们更接近常规Windows metro应用程序。



这是我的导航代码。

I have just moved over to making apps for Windows Phone 8.1 using the RT APIs having formerly exclusively used Silverlight for Windows Phone apps.

I am creating a basic app with different pages. The main page is just a menu page with a list of buttons that take the user to different pages. The issue I am having is getting the user back to the main page from other pages.

I have two types of sub-pages.

The first ones are just plain pages and when the user hits the back button they get taken back to the main page.
However I have some pages with a pivot control. When the user hits the back button they are taken into the previous app. (My app is still running in the background).

Could someone explain why only the pages with pivot controls take the user out of my app when they hit the back button instead of the previous page (in this case main page)?

Any help is much appreciated, I have done some searching and I am aware that the navigation model has changed in Windows Phone 8 then in Windows Phone 8.1 RT apps to align them closer to regular Windows metro apps.

Here is my code for navigation.

private void Button_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
        {
              this.Frame.Navigate(typeof(about));
                  // This goes to a page with no pivot control and the user can navigate back fine.
        }


        private void partAbutton_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
        {
            this.Frame.Navigate(typeof(parta));
                // This goes to a page with a pivot control and the user cannot navigate back the main page when they hit the back button.
        }

推荐答案

您必须将BackPressedEventArgs.Handled属性设置为true。如果您的应用程序位于其第一页并且无法向后导航,则不应处理该事件,操作系统将暂停您的应用程序。

首先,您必须启用NavigationCacheMode然后添加事件后退按钮的处理程序按下到页面的OnNavigatedTo方法,在方法中你可以使用导航堆栈导航到后页。

这是完整的代码。



You have to set the BackPressedEventArgs.Handled property to true. If your app is at its first page and can't navigate backwards, you should not handle the event and the operating system will suspend your app.
First you have to enable the NavigationCacheMode and then add an event handler for back button press to OnNavigatedTo method of the page and in the method you can navigate to back page by using the navigation stack.
Here is the complete code for that.

public BlankPage2()
  {
      this.InitializeComponent();
      this.NavigationCacheMode = NavigationCacheMode.Enabled;
  }


  protected override void OnNavigatedTo(NavigationEventArgs e)
  {


      HardwareButtons.BackPressed += this.HardwareButtons_BackPressed;
  }


  protected override void OnNavigatedFrom(NavigationEventArgs e)
  {
      HardwareButtons.BackPressed -= this.HardwareButtons_BackPressed;
  }



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

      if (frame.CanGoBack)
      {
          frame.GoBack();
          e.Handled = true;
      }
  }


这篇关于在Windows Phone 8.1 RT App中导航时出现后退按钮问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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