处理返回导航Windows 10 (UWP)
本文关键字:UWP Windows 返回 导航 处理 | 更新日期: 2023-09-27 18:06:40
在我的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
事件后,它关闭了应用程序。
似乎在主页上运行一些默认的返回按钮行为…
我该如何修复它?在Windows10中,他们是否添加了新的事件来处理返回导航?
(更新)
现在我发现在Windows 10中使用SystemNavigationManager
而不是Input.HardwareButtons.BackPressed
更好。
SystemNavigationManager currentView = SystemNavigationManager.GetForCurrentView();
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
。使用名称空间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
希望这对某人有帮助!
你需要通过设置BackPressedEventArgs的handled属性为true来告诉系统你处理了backbutton按下。
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();
}
}
遵循这些步骤:
-
在你的页面中添加两个全局变量,如下所示
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 }
p。-在添加新页面时,您可能需要使用BasicPage
而不是BlankPage
。
希望这将帮助…
试试这个。它将适用于框架返回导航。
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();
}
}
}
我认为这是因为你在你的页面上添加了HardwareButtons_BackPressed,而不是在app.xaml.cs.
在app. example .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/