Skip to content

Commit e2d9e93

Browse files
committed
Updated BladeView and BladeItem
1 parent d3a5ade commit e2d9e93

File tree

6 files changed

+142
-26
lines changed

6 files changed

+142
-26
lines changed

Microsoft.Toolkit.Uwp.UI.Controls/BladeView/BladeItem.Properties.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ public partial class BladeItem
3939
/// </summary>
4040
public static readonly DependencyProperty CloseButtonForegroundProperty = DependencyProperty.Register(nameof(CloseButtonForeground), typeof(Brush), typeof(BladeItem), new PropertyMetadata(new SolidColorBrush(Colors.Black)));
4141

42+
private WeakReference<BladeView> _parentBladeView;
43+
4244
/// <summary>
4345
/// Gets or sets the foreground color of the close button
4446
/// </summary>
@@ -84,7 +86,15 @@ public bool IsOpen
8486
set { SetValue(IsOpenProperty, value); }
8587
}
8688

87-
internal BladeView ParentBladeView { get; set; }
89+
internal BladeView ParentBladeView
90+
{
91+
get
92+
{
93+
this._parentBladeView.TryGetTarget(out var bladeView);
94+
return bladeView;
95+
}
96+
set => this._parentBladeView = new WeakReference<BladeView>(value);
97+
}
8898

8999
private static void IsOpenChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
90100
{

Microsoft.Toolkit.Uwp.UI.Controls/BladeView/BladeItemAutomationPeer.cs

Lines changed: 84 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5-
using System.Linq;
65
using Microsoft.Toolkit.Uwp.UI.Controls;
6+
using Microsoft.Toolkit.Uwp.UI.Extensions;
7+
using Windows.UI.Xaml.Automation;
78
using Windows.UI.Xaml.Automation.Peers;
9+
using Windows.UI.Xaml.Controls;
810

911
namespace Microsoft.Toolkit.Uwp.UI.Automation.Peers
1012
{
@@ -35,7 +37,7 @@ private BladeItem OwnerBladeItem
3537
/// <returns>The control type.</returns>
3638
protected override AutomationControlType GetAutomationControlTypeCore()
3739
{
38-
return AutomationControlType.Custom;
40+
return AutomationControlType.ListItem;
3941
}
4042

4143
/// <summary>
@@ -59,25 +61,97 @@ protected override string GetClassNameCore()
5961
/// </returns>
6062
protected override string GetNameCore()
6163
{
62-
int? index = this.OwnerBladeItem.ParentBladeView.GetBladeItems().ToList().IndexOf(this.OwnerBladeItem);
64+
string name = AutomationProperties.GetName(this.OwnerBladeItem);
65+
if (!string.IsNullOrEmpty(name))
66+
{
67+
return name;
68+
}
6369

64-
string name = base.GetNameCore();
70+
name = this.OwnerBladeItem.Name;
6571
if (!string.IsNullOrEmpty(name))
6672
{
67-
return $"{name} {index}";
73+
return name;
6874
}
6975

70-
if (this.OwnerBladeItem != null && !string.IsNullOrEmpty(this.OwnerBladeItem.Name))
76+
TextBlock textBlock = this.OwnerBladeItem.FindDescendant<TextBlock>();
77+
if (textBlock != null)
7178
{
72-
return this.OwnerBladeItem.Name;
79+
return textBlock.Text;
7380
}
7481

75-
if (string.IsNullOrEmpty(name))
82+
name = base.GetNameCore();
83+
if (!string.IsNullOrEmpty(name))
7684
{
77-
name = this.GetClassName();
85+
return name;
7886
}
7987

80-
return $"{name} {index}";
88+
return string.Empty;
89+
}
90+
91+
/// <summary>
92+
/// Called by GetAutomationId that gets the **AutomationId** of the element that is associated with the automation peer.
93+
/// </summary>
94+
/// <returns>
95+
/// The string that contains the automation ID.
96+
/// </returns>
97+
protected override string GetAutomationIdCore()
98+
{
99+
string automationId = base.GetAutomationIdCore();
100+
if (!string.IsNullOrEmpty(automationId))
101+
{
102+
return automationId;
103+
}
104+
105+
if (this.OwnerBladeItem != null)
106+
{
107+
return this.GetNameCore();
108+
}
109+
110+
return string.Empty;
111+
}
112+
113+
/// <summary>
114+
/// Returns the size of the set where the element that is associated with the automation peer is located.
115+
/// </summary>
116+
/// <returns>
117+
/// The size of the set.
118+
/// </returns>
119+
protected override int GetSizeOfSetCore()
120+
{
121+
int sizeOfSet = base.GetSizeOfSetCore();
122+
123+
if (sizeOfSet != -1)
124+
{
125+
return sizeOfSet;
126+
}
127+
128+
BladeItem owner = this.OwnerBladeItem;
129+
BladeView parent = owner.ParentBladeView;
130+
sizeOfSet = parent.Items.Count;
131+
132+
return sizeOfSet;
133+
}
134+
135+
/// <summary>
136+
/// Returns the ordinal position in the set for the element that is associated with the automation peer.
137+
/// </summary>
138+
/// <returns>
139+
/// The ordinal position in the set.
140+
/// </returns>
141+
protected override int GetPositionInSetCore()
142+
{
143+
int positionInSet = base.GetPositionInSetCore();
144+
145+
if (positionInSet != -1)
146+
{
147+
return positionInSet;
148+
}
149+
150+
BladeItem owner = this.OwnerBladeItem;
151+
BladeView parent = owner.ParentBladeView;
152+
positionInSet = parent.IndexFromContainer(owner);
153+
154+
return positionInSet;
81155
}
82156
}
83157
}

Microsoft.Toolkit.Uwp.UI.Controls/BladeView/BladeView.cs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -212,13 +212,5 @@ private void ItemsVectorChanged(IObservableVector<object> sender, IVectorChanged
212212
GetScrollViewer()?.ChangeView(_scrollViewer.ScrollableWidth, null, null);
213213
}
214214
}
215-
216-
internal IEnumerable<BladeItem> GetBladeItems()
217-
{
218-
return Enumerable
219-
.Range(0, Items.Count)
220-
.Select(idx => (BladeItem)ContainerFromIndex(idx))
221-
.Where(i => i != null);
222-
}
223215
}
224216
}

Microsoft.Toolkit.Uwp.UI.Controls/BladeView/BladeViewAutomationPeer.cs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
using System.Collections.Generic;
66
using Microsoft.Toolkit.Uwp.UI.Controls;
7+
using Windows.UI.Xaml.Automation;
78
using Windows.UI.Xaml.Automation.Peers;
89
using Windows.UI.Xaml.Controls;
910

@@ -39,7 +40,7 @@ private BladeView OwningBladeView
3940
/// <returns>The control type.</returns>
4041
protected override AutomationControlType GetAutomationControlTypeCore()
4142
{
42-
return AutomationControlType.Custom;
43+
return AutomationControlType.List;
4344
}
4445

4546
/// <summary>
@@ -63,23 +64,25 @@ protected override string GetClassNameCore()
6364
/// </returns>
6465
protected override string GetNameCore()
6566
{
66-
string name = base.GetNameCore();
67+
string name = AutomationProperties.GetName(this.OwningBladeView);
6768
if (!string.IsNullOrEmpty(name))
6869
{
6970
return name;
7071
}
7172

72-
if (this.OwningBladeView != null)
73+
name = this.OwningBladeView.Name;
74+
if (!string.IsNullOrEmpty(name))
7375
{
74-
name = this.OwningBladeView.Name;
76+
return name;
7577
}
7678

77-
if (string.IsNullOrEmpty(name))
79+
name = base.GetNameCore();
80+
if (!string.IsNullOrEmpty(name))
7881
{
79-
name = this.GetClassName();
82+
return name;
8083
}
8184

82-
return name;
85+
return string.Empty;
8386
}
8487

8588
/// <summary>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using Windows.UI.Xaml.Automation;
6+
using Microsoft.Toolkit.Uwp.UI.Controls;
7+
using Microsoft.VisualStudio.TestTools.UnitTesting;
8+
using Microsoft.VisualStudio.TestTools.UnitTesting.AppContainer;
9+
using Windows.UI.Xaml.Automation.Peers;
10+
using Microsoft.Toolkit.Uwp.UI.Automation.Peers;
11+
12+
namespace UnitTests.UWP.UI.Controls
13+
{
14+
[TestClass]
15+
[TestCategory("Test_BladeView")]
16+
public class Test_BladeView
17+
{
18+
[UITestMethod]
19+
public void ShouldConfigureBladeViewAutomationPeer()
20+
{
21+
const string automationName = "MyAutomationBlades";
22+
const string name = "MyBlades";
23+
24+
var bladeView = new BladeView();
25+
var bladeViewAutomationPeer = FrameworkElementAutomationPeer.CreatePeerForElement(bladeView) as BladeViewAutomationPeer;
26+
27+
Assert.IsNotNull(bladeViewAutomationPeer, "Verify that the AutomationPeer is BladeViewAutomationPeer.");
28+
29+
bladeView.Name = name;
30+
Assert.IsTrue(bladeViewAutomationPeer.GetName().Contains(name), "Verify that the UIA name contains the given Name of the BladeView.");
31+
32+
bladeView.SetValue(AutomationProperties.NameProperty, automationName);
33+
Assert.IsTrue(bladeViewAutomationPeer.GetName().Contains(automationName), "Verify that the UIA name contains the given AutomationProperties.Name of the BladeView.");
34+
}
35+
}
36+
}

UnitTests/UnitTests.UWP/UnitTests.UWP.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@
179179
<Compile Include="PrivateType.cs" />
180180
<Compile Include="Properties\AssemblyInfo.cs" />
181181
<Compile Include="Helpers\Test_WeakEventListener.cs" />
182+
<Compile Include="UI\Controls\Test_BladeView.cs" />
182183
<Compile Include="UI\Controls\Test_RadialGauge.cs" />
183184
<Compile Include="UI\Controls\Test_TextToolbar_Localization.cs" />
184185
<Compile Include="UI\Controls\Test_InfiniteCanvas_Regression.cs" />

0 commit comments

Comments
 (0)