C# 处理返回导航窗口10(UWP)

C# 处理返回导航窗口10(UWP),c#,navigation,windows-10,windows-10-mobile,C#,Navigation,Windows 10,Windows 10 Mobile,在我的Xaml页面中,我有一个框架 我正在尝试一个backButton事件,以便在帧内导航 所以我试着使用这段代码 public MainPage(){ this.InitializeComponent(); if(Windows.Foundation.Metadata.ApiInformation.IsTypePresent("Windows.Phone.UI.Input.HardwareButtons")) { Windows.Phone.UI.Input.H

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

我正在尝试一个backButton事件,以便在帧内导航

所以我试着使用这段代码

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();
}
但在手机中,在执行
硬件按钮\u后按事件关闭应用程序

似乎在主页上运行一些默认的后退按钮行为

我怎样才能修好它?在Windows10中,他们是否添加了新事件来处理后台导航


[更新]

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

SystemNavigationManager currentView = SystemNavigationManager.GetForCurrentView();

我认为这是因为您在页面中添加了硬件按钮,而不是在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();
    }
}
现在,手机的后退按钮可以在任何页面上使用

然后,如果要在任何页面中添加特定按钮,请执行以下操作:

在特定页面(或每个页面,如果需要):

资料来源:
遵循以下步骤:

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

    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
    }
    
ps.-为此,在添加新页面时,您可能需要使用
BasicPage
而不是
BlankPage


希望这会有帮助

您需要通过将BackPressedEventArgs的handled属性设置为true来告诉系统您已处理backbutton press

  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();
        }
  }

试试这个。它可以用于帧后导航

  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();
        }
    }

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

因为
SystemNavigationManager
Windows通用平台的一部分,所以它受到运行在windows10上的所有设备系列(包括移动设备和PC)的支持

单页
如果您只想处理单个页面的导航。遵循以下步骤

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

using Windows.UI.Core;
using Windows.UI.Core;
步骤2。当前视图的注册回请求事件。最好的地方是类的主构造函数在
InitializeComponent()之后

步骤3。处理请求的事件

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

处理所有视图的所有backbutton的最佳位置是
App.xaml.cs

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

using Windows.UI.Core;
using Windows.UI.Core;
步骤2。当前视图的注册回请求事件。最好的地方是在
窗口之前的
OnLaunched
。当前。激活

protected override void OnLaunched(LaunchActivatedEventArgs e)
{
    ...
    SystemNavigationManager.GetForCurrentView().BackRequested += OnBackRequested;
    Window.Current.Activate();
}
    
步骤3。处理请求的事件

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

参考-


希望这对别人有帮助

NavigationHelper
在哪里?正如您所知,在Windows10中,没有像windows phone 8.1这样的模板可以包含
NavigationHelper
。。。如果你知道。。。在哪里可以获得
NavigationHelper
?在windows 10中没有
BasicPage
。。。它只是
BlackPage
您可以从任何现有的wp8.1项目或其他项目模板中获取该文件。insideFrame不在App.xaml.cs范围内。谢谢您的提示。编辑了我的答案以修复此问题。可以在以下博客中找到
SystemNavigationManager
的用法:@Shahriar也适用于Windows 10 Mobile。因为我选中了,所以Windows Mobile中不显示“后退”按钮。