From 11130370137a7ca0b12087c93f6510209c7af989 Mon Sep 17 00:00:00 2001 From: Kenny Guo Date: Fri, 30 Jul 2021 10:28:37 +0800 Subject: [PATCH] Use automation peer to trigger back button click in accelerator --- src/Calculator/Views/MainPage.xaml.cs | 2 +- src/Calculator/Views/TitleBar.xaml.cs | 18 +++++++----------- 2 files changed, 8 insertions(+), 12 deletions(-) diff --git a/src/Calculator/Views/MainPage.xaml.cs b/src/Calculator/Views/MainPage.xaml.cs index f6cc0aee..1862b197 100644 --- a/src/Calculator/Views/MainPage.xaml.cs +++ b/src/Calculator/Views/MainPage.xaml.cs @@ -578,7 +578,7 @@ namespace CalculatorApp NarratorNotifier.Announce(announcement); } - private void TitleBarBackButtonClick(object sender) + private void TitleBarBackButtonClick(object sender, RoutedEventArgs e) { CloseSettingsPopup(); } diff --git a/src/Calculator/Views/TitleBar.xaml.cs b/src/Calculator/Views/TitleBar.xaml.cs index e498a878..2797e5bb 100644 --- a/src/Calculator/Views/TitleBar.xaml.cs +++ b/src/Calculator/Views/TitleBar.xaml.cs @@ -7,6 +7,8 @@ using Windows.UI; using Windows.UI.Core; using Windows.UI.ViewManagement; using Windows.UI.Xaml; +using Windows.UI.Xaml.Automation.Peers; +using Windows.UI.Xaml.Automation.Provider; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Media; @@ -51,9 +53,7 @@ namespace CalculatorApp })); public event Windows.UI.Xaml.RoutedEventHandler AlwaysOnTopClick; - - public delegate void BackButtonClickEventHandler(object sender); - public event BackButtonClickEventHandler BackButtonClick; + public event Windows.UI.Xaml.RoutedEventHandler BackButtonClick; private void OnLoaded(object sender, RoutedEventArgs e) { @@ -103,7 +103,9 @@ namespace CalculatorApp { if (!e.Handled && BackButton.IsEnabled) { - InvokeBackButton(sender) ; + var buttonPeer = new ButtonAutomationPeer(BackButton); + IInvokeProvider invokeProvider = buttonPeer.GetPattern(PatternInterface.Invoke) as IInvokeProvider; + invokeProvider.Invoke(); e.Handled = true; } @@ -229,15 +231,9 @@ namespace CalculatorApp AlwaysOnTopClick?.Invoke(this, e); } - // Called when BackButton invoked, for example, by clicking, access key and etc. - private void InvokeBackButton(object sender) - { - BackButtonClick?.Invoke(this); - } - private void BackButton_Click(object sender, RoutedEventArgs e) { - InvokeBackButton(sender); + BackButtonClick?.Invoke(this, e); } // Dependency properties for the color of the system title bar buttons