如何在 UWP 中处理 HardwareButtons.BackPressed?
How to handled HardwareButtons.BackPressed in UWP?
我有带 Prism 和 AppShell 的 UWP 应用程序。
我想在通过 BackButton 退出之前添加确认对话框。
我试过这个:
protected override Task OnLaunchApplicationAsync(LaunchActivatedEventArgs args)
{
...
SystemNavigationManager.GetForCurrentView().BackRequested += App_BackRequested;
...
}
private void App_BackRequested(object sender, BackRequestedEventArgs e)
{
Frame rootFrame = Window.Current.Content as Frame;
if (rootFrame == null)
{
return;
}
if (rootFrame.CanGoBack && e.Handled == false)
{
<add confirm dialog here>
e.Handled = true;
}
}
但 rootFrame 始终为 null,如果历史堆栈为空并且我按下 BackButton 应用程序关闭,即使我这样做也是如此:
private void App_BackRequested(object sender, BackRequestedEventArgs e)
{
e.Handled = true;
}
我也试过了
HardwareButtons.BackPressed += App_BackRequested;
也无济于事。
在App.xaml.cs中添加代码 -
private async void HardwareButtons_BackPressed(object sender, BackPressedEventArgs e)
{
e.Handled = true;
Frame rootFrame = Window.Current.Content as Frame;
if (rootFrame.CanGoBack && rootFrame != null)
{
rootFrame.GoBack();
}
else
{
var msg = new MessageDialog("Confirm Close");
var okBtn = new UICommand("OK");
var cancelBtn = new UICommand("Cancel");
msg.Commands.Add(okBtn);
msg.Commands.Add(cancelBtn);
IUICommand result = await msg.ShowAsync();
if (result != null && result.Label == "OK")
{
Application.Current.Exit();
}
}
}
在 App.xaml.cs 的构造函数中添加这一行 -
HardwareButtons.BackPressed += HardwareButtons_BackPressed;
尝试将以下这些行添加到 OnLaunchApplicationAsync
方法
protected override Task OnLaunchApplicationAsync(LaunchActivatedEventArgs args)
{
//......
DeviceGestureService.GoBackRequested += (s, e) =>
{
e.Handled = true;
e.Cancel = true;
if (NavigationService.CanGoBack())
{
NavigationService.GoBack();
}
else
{
// PUT YOUR LOGIC HERE (Confirmation dialog before exit)
Application.Current.Exit();
}
};
//......
}
我有带 Prism 和 AppShell 的 UWP 应用程序。 我想在通过 BackButton 退出之前添加确认对话框。 我试过这个:
protected override Task OnLaunchApplicationAsync(LaunchActivatedEventArgs args)
{
...
SystemNavigationManager.GetForCurrentView().BackRequested += App_BackRequested;
...
}
private void App_BackRequested(object sender, BackRequestedEventArgs e)
{
Frame rootFrame = Window.Current.Content as Frame;
if (rootFrame == null)
{
return;
}
if (rootFrame.CanGoBack && e.Handled == false)
{
<add confirm dialog here>
e.Handled = true;
}
}
但 rootFrame 始终为 null,如果历史堆栈为空并且我按下 BackButton 应用程序关闭,即使我这样做也是如此:
private void App_BackRequested(object sender, BackRequestedEventArgs e)
{
e.Handled = true;
}
我也试过了
HardwareButtons.BackPressed += App_BackRequested;
也无济于事。
在App.xaml.cs中添加代码 -
private async void HardwareButtons_BackPressed(object sender, BackPressedEventArgs e)
{
e.Handled = true;
Frame rootFrame = Window.Current.Content as Frame;
if (rootFrame.CanGoBack && rootFrame != null)
{
rootFrame.GoBack();
}
else
{
var msg = new MessageDialog("Confirm Close");
var okBtn = new UICommand("OK");
var cancelBtn = new UICommand("Cancel");
msg.Commands.Add(okBtn);
msg.Commands.Add(cancelBtn);
IUICommand result = await msg.ShowAsync();
if (result != null && result.Label == "OK")
{
Application.Current.Exit();
}
}
}
在 App.xaml.cs 的构造函数中添加这一行 -
HardwareButtons.BackPressed += HardwareButtons_BackPressed;
尝试将以下这些行添加到 OnLaunchApplicationAsync
方法
protected override Task OnLaunchApplicationAsync(LaunchActivatedEventArgs args)
{
//......
DeviceGestureService.GoBackRequested += (s, e) =>
{
e.Handled = true;
e.Cancel = true;
if (NavigationService.CanGoBack())
{
NavigationService.GoBack();
}
else
{
// PUT YOUR LOGIC HERE (Confirmation dialog before exit)
Application.Current.Exit();
}
};
//......
}