Skip to content

Commit a706ca6

Browse files
committed
Improved implementation for Carousel automation peer
1 parent 84d9e00 commit a706ca6

File tree

5 files changed

+27
-19
lines changed

5 files changed

+27
-19
lines changed

Microsoft.Toolkit.Uwp.SampleApp/Data/PhotoDataItem.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,10 @@ public class PhotoDataItem
1111
public string Category { get; set; }
1212

1313
public string Thumbnail { get; set; }
14+
15+
public override string ToString()
16+
{
17+
return Title;
18+
}
1419
}
1520
}

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

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -543,14 +543,6 @@ private void OnCarouselItemSelected(object sender, EventArgs e)
543543
SelectedItem = ItemFromContainer(item);
544544
}
545545

546-
internal IEnumerable<CarouselItem> GetCarouselItems()
547-
{
548-
return Enumerable
549-
.Range(0, Items.Count)
550-
.Select(idx => (CarouselItem)ContainerFromIndex(idx))
551-
.Where(i => i != null);
552-
}
553-
554546
internal void SetSelectedItem(CarouselItem owner)
555547
{
556548
var item = ItemFromContainer(owner);

Microsoft.Toolkit.Uwp.UI.Controls/Carousel/CarouselAutomationPeer.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,9 @@ private Carousel OwningCarousel
4747
/// <returns>An array of UI Automation providers.</returns>
4848
public IRawElementProviderSimple[] GetSelection()
4949
{
50-
CarouselItem selectedCarouselItem = OwningCarousel.GetCarouselItems().FirstOrDefault(x => x.IsSelected);
51-
return selectedCarouselItem != null
52-
? new[] { this.ProviderFromPeer(FromElement(selectedCarouselItem)) }
53-
: new IRawElementProviderSimple[] { };
50+
return OwningCarousel.ContainerFromItem(this.OwningCarousel.SelectedItem) is CarouselItem selectedCarouselItem
51+
? new[] { this.ProviderFromPeer(FromElement(selectedCarouselItem)) }
52+
: new IRawElementProviderSimple[] { };
5453
}
5554

5655
/// <summary>

Microsoft.Toolkit.Uwp.UI.Controls/Carousel/CarouselItem.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ public class CarouselItem : SelectorItem
2323
private const string SelectedState = "Selected";
2424
private const string NormalState = "Normal";
2525

26+
private WeakReference<Carousel> parentCarousel;
27+
2628
/// <summary>
2729
/// Initializes a new instance of the <see cref="CarouselItem"/> class.
2830
/// </summary>
@@ -34,7 +36,15 @@ public CarouselItem()
3436
RegisterPropertyChangedCallback(SelectorItem.IsSelectedProperty, OnIsSelectedChanged);
3537
}
3638

37-
internal Carousel ParentCarousel { get; set; }
39+
internal Carousel ParentCarousel
40+
{
41+
get
42+
{
43+
this.parentCarousel.TryGetTarget(out var carousel);
44+
return carousel;
45+
}
46+
set => this.parentCarousel = new WeakReference<Carousel>(value);
47+
}
3848

3949
/// <inheritdoc/>
4050
protected override void OnPointerEntered(PointerRoutedEventArgs e)

Microsoft.Toolkit.Uwp.UI.Controls/Carousel/CarouselItemAutomationPeer.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
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;
76
using Windows.UI.Xaml.Automation.Peers;
87
using Windows.UI.Xaml.Automation.Provider;
@@ -56,23 +55,26 @@ public void AddToSelection()
5655
{
5756
CarouselItem owner = this.OwnerCarouselItem;
5857
Carousel parent = owner.ParentCarousel;
59-
parent.SetSelectedItem(owner);
58+
parent?.SetSelectedItem(owner);
6059
}
6160

6261
/// <summary>Removes the current element from the collection of selected items.</summary>
6362
public void RemoveFromSelection()
6463
{
6564
CarouselItem owner = this.OwnerCarouselItem;
6665
Carousel parent = owner.ParentCarousel;
67-
parent.SelectedItem = null;
66+
if (parent != null)
67+
{
68+
parent.SelectedItem = null;
69+
}
6870
}
6971

7072
/// <summary>Clears any existing selection and then selects the current element.</summary>
7173
public void Select()
7274
{
7375
CarouselItem owner = this.OwnerCarouselItem;
7476
Carousel parent = owner.ParentCarousel;
75-
parent.SetSelectedItem(owner);
77+
parent?.SetSelectedItem(owner);
7678
}
7779

7880
/// <summary>
@@ -105,12 +107,12 @@ protected override string GetClassNameCore()
105107
/// </returns>
106108
protected override string GetNameCore()
107109
{
108-
int? index = this.OwnerCarouselItem.ParentCarousel.GetCarouselItems().ToList().IndexOf(this.OwnerCarouselItem);
110+
int? index = this.OwnerCarouselItem.ParentCarousel?.IndexFromContainer(this.OwnerCarouselItem);
109111

110112
string name = base.GetNameCore();
111113
if (!string.IsNullOrEmpty(name))
112114
{
113-
return $"{name} {index}";
115+
return $"{name}";
114116
}
115117

116118
if (this.OwnerCarouselItem != null && !string.IsNullOrEmpty(this.OwnerCarouselItem.Name))

0 commit comments

Comments
 (0)