From 40b3b428e457a47b9e8e59e966433925ef9364a5 Mon Sep 17 00:00:00 2001 From: Shane Neuville Date: Thu, 12 Nov 2020 11:54:17 -0600 Subject: [PATCH] Fix multiple events on toolbar items (#12797) --- .../Issue7181.cs | 12 +++++---- .../AccessibilityExtensions.cs | 11 ++++++++ .../Shell/ShellStyles.xaml | 3 +-- .../Shell/ShellToolbarItemRenderer.cs | 27 +++++++++++++++++-- 4 files changed, 44 insertions(+), 9 deletions(-) diff --git a/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue7181.cs b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue7181.cs index 017d03ac2..faa151262 100644 --- a/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue7181.cs +++ b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue7181.cs @@ -31,9 +31,11 @@ namespace Xamarin.Forms.Controls.Issues { var page = CreateContentPage("Test page"); - _toolbarItem = new ToolbarItem(DefaultToolbarItemText, string.Empty, OnToolbarClicked) + _toolbarItem = new ToolbarItem() { - AutomationId = ToolbarBtn + Text = DefaultToolbarItemText, + AutomationId = ToolbarBtn, + Command = new Command(OnToolbarClicked) }; page.ToolbarItems.Add(_toolbarItem); @@ -58,20 +60,20 @@ namespace Xamarin.Forms.Controls.Issues private void OnToolbarClicked() => _toolbarItem.Text = $"{AfterClickToolbarItemText} {_clicks++}"; -#if UITEST && __ANDROID__ +#if UITEST && (__ANDROID__ || __WINDOWS__) [Test] public void ShellToolbarItemTests() { var count = 0; var toolbarButton = RunningApp.WaitForElement(ToolbarBtn); - Assert.AreEqual(toolbarButton[0].Text, DefaultToolbarItemText); + Assert.AreEqual(DefaultToolbarItemText, toolbarButton[0].ReadText()); for (int i = 0; i < 5; i++) { RunningApp.Tap(ToolbarBtn); toolbarButton = RunningApp.WaitForElement(ToolbarBtn); - Assert.AreEqual($"{AfterClickToolbarItemText} {count++}", toolbarButton[0].Text); + Assert.AreEqual($"{AfterClickToolbarItemText} {count++}", toolbarButton[0].ReadText()); } RunningApp.Tap(SetToolbarIconBtn); diff --git a/Xamarin.Forms.Platform.UAP/AccessibilityExtensions.cs b/Xamarin.Forms.Platform.UAP/AccessibilityExtensions.cs index c4e78d63f..6f43a4d6a 100644 --- a/Xamarin.Forms.Platform.UAP/AccessibilityExtensions.cs +++ b/Xamarin.Forms.Platform.UAP/AccessibilityExtensions.cs @@ -123,5 +123,16 @@ namespace Xamarin.Forms.Platform.UWP } + internal static void SetAutomationProperties( + this FrameworkElement frameworkElement, + Element element, + string defaultName = null) + { + frameworkElement.SetAutomationPropertiesAutomationId(element?.AutomationId); + frameworkElement.SetAutomationPropertiesName(element, defaultName); + frameworkElement.SetAutomationPropertiesHelpText(element); + frameworkElement.SetAutomationPropertiesLabeledBy(element); + frameworkElement.SetAutomationPropertiesAccessibilityView(element); + } } } diff --git a/Xamarin.Forms.Platform.UAP/Shell/ShellStyles.xaml b/Xamarin.Forms.Platform.UAP/Shell/ShellStyles.xaml index 489ef4db0..457494529 100644 --- a/Xamarin.Forms.Platform.UAP/Shell/ShellStyles.xaml +++ b/Xamarin.Forms.Platform.UAP/Shell/ShellStyles.xaml @@ -28,8 +28,7 @@ - + diff --git a/Xamarin.Forms.Platform.UAP/Shell/ShellToolbarItemRenderer.cs b/Xamarin.Forms.Platform.UAP/Shell/ShellToolbarItemRenderer.cs index 7f96fcbdb..7231f97ed 100644 --- a/Xamarin.Forms.Platform.UAP/Shell/ShellToolbarItemRenderer.cs +++ b/Xamarin.Forms.Platform.UAP/Shell/ShellToolbarItemRenderer.cs @@ -10,6 +10,16 @@ namespace Xamarin.Forms.Platform.UWP { public class ShellToolbarItemRenderer : Windows.UI.Xaml.Controls.Button { + + public static readonly DependencyProperty ToolbarItemProperty = + DependencyProperty.Register("ToolbarItem", typeof(ToolbarItem), typeof(ShellToolbarItemRenderer), new PropertyMetadata(null, OnToolbarItemChanged)); + + static void OnToolbarItemChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) + { + ((ShellToolbarItemRenderer)d) + .ToolbarItemChanged(e.OldValue as ToolbarItem, e.NewValue as ToolbarItem); + } + public ShellToolbarItemRenderer() { Xamarin.Forms.Shell.VerifyShellUWPFlagEnabled(nameof(ShellToolbarItemRenderer)); @@ -28,7 +38,20 @@ namespace Xamarin.Forms.Platform.UWP set { SetValue(ToolbarItemProperty, value); } } - public static readonly DependencyProperty ToolbarItemProperty = - DependencyProperty.Register("ToolbarItem", typeof(ToolbarItem), typeof(ShellToolbarItemRenderer), new PropertyMetadata(null)); + void ToolbarItemChanged(ToolbarItem oldItem, ToolbarItem newItem) + { + if(oldItem != null) + oldItem.PropertyChanged -= ToolbarItemPropertyChanged; + + this.SetAutomationProperties(newItem, defaultName: newItem?.Text); + + if (newItem != null) + newItem.PropertyChanged += ToolbarItemPropertyChanged; + + void ToolbarItemPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e) + { + this.SetAutomationProperties(newItem, defaultName: newItem?.Text); + } + } } }