关于返回键,放在标题栏是目前较为完美的一种方案。继前一篇的Hello World,博主进行一些修改实现该方法。
- - - - - - - - - - - - - - - - - - - - - - - 我是万恶的分割线- - - - - - - - - - - - - - - - - - -
在OnLaunched的方法中加
Windows.UI.Core.SystemNavigationManager.GetForCurrentView().BackRequested += BackRequested;
紧接着在加一段代码
SystemNavigationManager.GetForCurrentView().AppViewBackButtonVisibility = rootFrame.CanGoBack ? AppViewBackButtonVisibility.Visible : Windows.UI.Core.AppViewBackButtonVisibility.Collapsed;
rootFrame.Navigated += OnNavigated;
我们在继续把代码写完用两个方法来适配前面写的两段代码
private void OnNavigated(object sender, NavigationEventArgs e)
{
SystemNavigationManager.GetForCurrentView().AppViewBackButtonVisibility = ((Frame)sender).CanGoBack ?
AppViewBackButtonVisibility.Visible : AppViewBackButtonVisibility.Collapsed;
}
private void BackRequested(object sender, BackRequestedEventArgs e)
{
Frame rootFrame = Window.Current.Content as Frame;
if (rootFrame == null)
return;
if (rootFrame.CanGoBack && e.Handled == false)
{
e.Handled = true;
rootFrame.GoBack();
}
}
为了能够看见效果,我们另外添加了一个页面
然后主界面的代码如下
基本的都做完了我们来看一下效果怎么样
时间: 2024-11-08 19:00:13