处理返回导航 Windows 10 (UWP)

Posted

技术标签:

【中文标题】处理返回导航 Windows 10 (UWP)【英文标题】:Handling Back Navigation Windows 10 (UWP) 【发布时间】:2015-10-28 05:13:04 【问题描述】:

在我的 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();

但在手机中执行HardwareButtons_BackPressed 事件后它会关闭应用程序。

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

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


[更新]

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

SystemNavigationManager currentView = SystemNavigationManager.GetForCurrentView();

【问题讨论】:

SystemNavigationManager的使用可以在以下博客中找到:wintellect.com/devcenter/jprosise/… @Shahriar 这也适用于 Windows 10 Mobile。因为我检查过,Windows Mobile 中不显示后退按钮。 【参考方案1】:

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

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

单页


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

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

using Windows.UI.Core;

第 2 步。 为当前视图注册返回请求事件。最好的地方是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;
    


参考资料-Handle back button pressed in UWP

希望这对某人有帮助!

【讨论】:

【参考方案2】:

您需要通过将 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();
        
  

【讨论】:

【参考方案3】:

按照以下步骤操作:

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

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

希望这会有所帮助..!

【讨论】:

NavigationHelper 在哪里?如您所知,在 Windows10 中没有模板这样的 windows phone 8.1 里面有NavigationHelper...如果您知道...我在哪里可以得到NavigationHelper 在 Windows 10 中没有BasicPage ...只有BlackPage 您可以从任何现有的 wp8.1 项目中获取文件,也可以从其他项目模板中获取文件。【参考方案4】:

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

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


【讨论】:

【参考方案5】:

我认为这是因为您在页面中添加了 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/

【讨论】:

insideFrame 不在 App.xaml.cs 范围内 感谢 cosmo0 的提示。编辑我的答案来解决这个问题。

以上是关于处理返回导航 Windows 10 (UWP)的主要内容,如果未能解决你的问题,请参考以下文章

导航页面时是不是所有 UWP 应用都会泄漏内存?

Cordova Windows 10 UWP 应用程序中的黑屏

Windows 7正式停用,Windows 10 UWP v19.2控件工具更值得拥有

Windows 10 UWP:无法停止枢轴控制循环

如何处理为 UWP 按下的后退按钮

Xamarin UWP + Android ContentPage 导航 PushAsync