处理返回导航 Windows 10 (UWP)
Handling Back Navigation Windows 10 (UWP)
在我的 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();
}
但在 phone 中执行 HardwareButtons_BackPressed
事件后关闭应用程序。
似乎 运行 MainPage 上的某些默认后退按钮行为...
我该如何解决?在 Windows10 中,他们是否添加了新事件来处理向后导航?
[更新]
现在我发现在 Windows 10 中使用 SystemNavigationManager
比 Input.HardwareButtons.BackPressed
更好。
SystemNavigationManager currentView = SystemNavigationManager.GetForCurrentView();
我认为这是因为您在页面中添加了 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();
}
}
现在,phone 的后退按钮可以在任何页面上使用。
然后,如果您想在任何页面中添加一个特定的按钮:
在特定页面(如果需要,也可以是每个页面):
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();
}
}
遵循这些步骤ps:
在您的页面中添加两个全局变量,如下所示。
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 来告诉系统您处理了后退按钮按下操作。
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();
}
}
尝试 this.It 将适用于框架后退导航。
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 仅用于导航目的。
因为 SystemNavigationManager
是 Windows Universal Platform
的一部分,所以 Windows 10 上的所有设备系列 运行 都支持它,包括移动设备和 PC。
对于单页
如果您只想处理单个页面的导航。按照以下步骤进行
步骤 1。使用命名空间 Windows.UI.Core
using Windows.UI.Core;
步骤 2. 为当前视图注册返回请求事件。最好的地方是 InitializeComponent()
.
之后 class 的主要构造函数
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
希望对大家有所帮助!
在我的 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();
}
但在 phone 中执行 HardwareButtons_BackPressed
事件后关闭应用程序。
似乎 运行 MainPage 上的某些默认后退按钮行为...
我该如何解决?在 Windows10 中,他们是否添加了新事件来处理向后导航?
[更新]
现在我发现在 Windows 10 中使用 SystemNavigationManager
比 Input.HardwareButtons.BackPressed
更好。
SystemNavigationManager currentView = SystemNavigationManager.GetForCurrentView();
我认为这是因为您在页面中添加了 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();
}
}
现在,phone 的后退按钮可以在任何页面上使用。
然后,如果您想在任何页面中添加一个特定的按钮:
在特定页面(如果需要,也可以是每个页面):
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();
}
}
遵循这些步骤ps:
在您的页面中添加两个全局变量,如下所示。
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 来告诉系统您处理了后退按钮按下操作。
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();
}
}
尝试 this.It 将适用于框架后退导航。
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 仅用于导航目的。
因为 SystemNavigationManager
是 Windows 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
希望对大家有所帮助!