16

在我的 Xaml 页面中,我有一个框架。

我试图让 backButton 事件在 frame 内导航。

所以我尝试使用这段代码

public MainPage(){
    this.InitializeComponent();
    if(Windows.Foundation.Metadata.ApiInformation.IsTypePresent("Windows.Phone.UI.Input.HardwareButtons")) {
        Windows.Phone.UI.Input.HardwareButtons.BackPressed += HardwareButtons_BackPressed;
    }
}
private void HardwareButtons_BackPressed(object sender,BackPressedEventArgs e) {
    if(insideFrame.CanGoBack())insideFrame.GoBack();
    else  Application.Current.Exit();
}

但是在手机中完成HardwareButtons_BackPressed事件后它会关闭应用程序。

似乎在 MainPage 上运行了一些默认的后退按钮行为......

我该如何解决?在 Windows10 中,他们是否添加了新事件来处理后退导航?


[更新]

现在我发现SystemNavigationManager在 Windows 10中使用比在Input.HardwareButtons.BackPressed.

SystemNavigationManager currentView = SystemNavigationManager.GetForCurrentView();
4

5 回答 5

24

Windows 10 (UWP)在命名空间中包含SystemNavigationManagerWindows.UI.Core ,仅用于导航目的。

因为SystemNavigationManager它是 的一部分Windows Universal Platform,所以它受到在 Windows 10 上运行的所有设备系列的支持,包括移动设备和 PC。

对于单页


如果您只想处理单页导航。请按照以下步骤操作

步骤 1。使用命名空间Windows.UI.Core

using Windows.UI.Core;

步骤 2.为当前视图注册返回请求事件。最好的地方是 after 类的主要构造函数InitializeComponent()

public MainPage()
{
    this.InitializeComponent();
    //register back request event for current view
    SystemNavigationManager.GetForCurrentView().BackRequested += MainPage_BackRequested;
}

步骤 3.处理 BackRequested 事件

private void Food_BackRequested(object sender, BackRequestedEventArgs e)
{
    if (Frame.CanGoBack)
    {
        Frame.GoBack();
        e.Handled = true;
    }
}

在一个地方完成申请单rootFrame


处理所有视图的所有后退按钮的最佳位置是App.xaml.cs

步骤 1。使用命名空间Windows.UI.Core

using Windows.UI.Core;

步骤 2.为当前视图注册返回请求事件。最好的地方OnLaunched就在之前Window.Current.Activate

protected override void OnLaunched(LaunchActivatedEventArgs e)
{
    ...
    SystemNavigationManager.GetForCurrentView().BackRequested += OnBackRequested;
    Window.Current.Activate();
}
    

步骤 3.处理 BackRequested 事件

private void OnBackRequested(object sender, BackRequestedEventArgs e)
{
    Frame rootFrame = Window.Current.Content as Frame;
    if (rootFrame.CanGoBack)
    {
        rootFrame.GoBack();
        e.Handled = true;
    }
}

参考- 在 UWP 中按下手柄后退按钮

希望这对某人有帮助!

于 2016-03-08T19:04:55.343 回答
7

您需要通过将 BackPressedEventArgs 的 Handled 属性设置为 true 来告诉系统您处理了后退按钮按下。

  private void OnHardwareButtonsBackPressed(object sender, BackPressedEventArgs e)
  {
        // This is the missing line!
        e.Handled = true;

        // Close the App if you are on the startpage
        if (mMainFrame.CurrentSourcePageType == typeof(Startpage))
            App.Current.Exit();

        // Navigate back
        if (mMainFrame.CanGoBack)
        {
            mMainFrame.GoBack();
        }
  }
于 2015-08-05T13:10:26.197 回答
0

按着这些次序:

  • 在您的页面中添加两个全局变量,如下所示。

    private NavigationHelper navigationHelper;
    private RelayCommand _GoBackCommand;
    
  • 然后在特定页面的构造函数中添加以下代码。

        // below code is to override the back navigation
        // hardware back button press event from navigationHelper
        _GoBackCommand = new RelayCommand
            (
                () => this.CheckGoBack(),
                () => this.CanCheckGoBack()
            );
    
        navigationHelper.GoBackCommand = _GoBackCommand;
        // ---------
    
  • 然后添加我们刚刚在构造函数中声明的那些方法。

    private bool CanCheckGoBack()
    {
        // this should be always true to make sure the app handles back buton manually.
        return true;
    }
    
    private void CheckGoBack()
    {
        // this will be execute when back button will be pressed
    }
    

附言。- 为此,您可能需要使用BasicPage而不是BlankPage在添加新页面时使用。

希望这会有所帮助..!

于 2015-08-05T12:28:17.940 回答
0

试试这个。它将适用于框架后退导航。

  protected override void OnNavigatedTo(NavigationEventArgs e)
    {

      HardwareButtons.BackPressed += HardwareButtons_BackPressed;

    }


    void HardwareButtons_BackPressed(object sender, BackPressedEventArgs e)
    {

    Frame rootFrame = Window.Current.Content as Frame;

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

}
于 2015-08-10T05:52:01.973 回答
-1

我认为这是因为您在页面中添加了 HardwareButtons_BackPressed,而不是在 app.xaml.cs 中。

在 app.xaml.cs 中:

public App()
{
    this.InitializeComponent();
    this.Suspending += this.OnSuspending;

    HardwareButtons.BackPressed += HardwareButtons_BackPressed;
}

void HardwareButtons_BackPressed(object sender, BackPressedEventArgs e)
{
    Frame rootFrame = Window.Current.Content as Frame;

    if (rootFrame != null && rootFrame.CanGoBack)
    {
        e.Handled = true;
        rootFrame.GoBack();
    }
}

现在,您手机的后退按钮可以在任何页面上使用。

然后,如果您想在任何页面中添加一个特定的按钮:

在特定页面(或每个页面,如果你想):

public void btn_return_Tapped(object sender, TappedRoutedEventArgs e)
        {
            Frame rootFrame = Window.Current.Content as Frame;

            if (rootFrame != null && rootFrame.CanGoBack)
            {
                e.Handled = true;
                rootFrame.GoBack();
            }
        }

来源: http: //windowsapptutorials.com/tips/general-tips/handling-the-back-button-in-a-windows-phone-8-1-app/

于 2015-08-05T12:24:32.770 回答