Windows 10 如何处理为UWP按下的后退按钮
我曾经在WindowsPhone8.1XAML中使用硬件按钮API。然而,在UWP中,一些设备没有后退按钮。如何适应新的应用程序型号?您可以使用BackRequested事件处理BackRequest:Windows 10 如何处理为UWP按下的后退按钮,windows-10,windows-10-mobile,Windows 10,Windows 10 Mobile,我曾经在WindowsPhone8.1XAML中使用硬件按钮API。然而,在UWP中,一些设备没有后退按钮。如何适应新的应用程序型号?您可以使用BackRequested事件处理BackRequest: SystemNavigationManager.GetForCurrentView().BackRequested += OnBackRequested; if (App.MasterFrame.CanGoBack) { rootFrame.GoBack(); e.Handle
SystemNavigationManager.GetForCurrentView().BackRequested += OnBackRequested;
if (App.MasterFrame.CanGoBack)
{
rootFrame.GoBack();
e.Handled = true;
}
一点点解释答案。 您可以使用
Windows.UI.Core
名称空间
单页
如果您只想处理单个页面的导航。遵循以下步骤 步骤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;
}
}
参考-
希望这对别人有帮助 上述代码完全正确,但必须在rootFrame变量中添加frame对象。以下是:
private Frame _rootFrame;
protected override void OnLaunched(LaunchActivatedEventArgs e)
{
Frame rootFrame = Window.Current.Content as Frame;
if (Window.Current.Content==null)
{
_rootFrame = new Frame();
}
}
并将此_rootFrame传递给OnBackRequested方法。比如:
private void OnBackRequested(object sender, BackRequestedEventArgs
{
if (_rootFrame.CanGoBack)
{
_rootFrame.GoBack();
e.Handled = true;
}
}
此系统导航管理器位于哪里?我无法在Windows.UI.Core
命名空间中找到它。VS应该建议你这样做。这可能是因为我的目标是8.1。