我有Prism和AppShell的UWP應用程序。 我想在BackButton退出前添加確認對話框。 我嘗試這樣做:如何在UWP中處理HardwareButtons.BackPressed?
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,如果歷史堆棧空的,我按下返回按鈕的應用有即使imake的是:
private void App_BackRequested(object sender, BackRequestedEventArgs e)
{
e.Handled = true;
}
我也試過
HardwareButtons.BackPressed += App_BackRequested;
它也沒有幫助。
+1。如果您通過此解釋您實現的目標,則PO更易於理解您如何解決問題。使用Prism的 –
提供了處理硬件按鈕事件的能力,如(CameraButtonReleased,CameraButtonPressed,CameraButtonHalfPressed,GoBackRequested,.....)只是在OnLaunchApplicationAsync方法中實現它 –