在我的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();发布于 2015-08-05 13:10:26
您需要通过将BackPressedEventArgs的handled属性设置为true,告诉系统您处理了by按钮按下。
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();
}
}发布于 2016-03-08 19:04:55
Windows 10 (UWP)只为导航目的在Windows.UI.Core命名空间中包含Windows.UI.Core。
因为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;
}
}参考文献- 按下UWP的手柄后按钮
希望这对某人有帮助!
发布于 2015-08-05 12:28:17
遵循以下步骤:
ps。-为此,在添加新页面时,可能需要使用BasicPage而不是BlankPage。
希望这能帮上忙!
https://stackoverflow.com/questions/31832309
复制相似问题