Fix multiple events on toolbar items (#12797)
This commit is contained in:
Родитель
06ccda823a
Коммит
40b3b428e4
|
@ -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);
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -28,8 +28,7 @@
|
|||
<StackPanel Orientation="Horizontal" />
|
||||
</ItemsPanelTemplate>
|
||||
<DataTemplate x:Key="ShellToolbarItemTemplate">
|
||||
<xf:ShellToolbarItemRenderer ToolbarItem="{Binding}" Margin="0" Background="Transparent" BorderThickness="1"
|
||||
IsEnabled="{Binding IsEnabled}" Command="{Binding Command}" CommandParameter="{Binding CommandParameter}">
|
||||
<xf:ShellToolbarItemRenderer ToolbarItem="{Binding}" Margin="0" Background="Transparent" BorderThickness="1" IsEnabled="{Binding IsEnabled}">
|
||||
<Grid>
|
||||
<Grid.ColumnDefinitions>
|
||||
<ColumnDefinition Width="Auto"></ColumnDefinition>
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Загрузка…
Ссылка в новой задаче