Skip to content

Commit 2e3ccec

Browse files
Remove SwitchPresenter logic that would listen to changes in the collection or case values
Can't think of scenarios where this was actually useful and added a lot of complexity. Removing it for now for the core value the control provides and simplifies the structure. Samples from Sample App continue to work as expected.
1 parent 08bbe58 commit 2e3ccec

File tree

3 files changed

+6
-158
lines changed

3 files changed

+6
-158
lines changed

Microsoft.Toolkit.Uwp.UI.Controls.Primitives/SwitchPresenter/Case.cs

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,6 @@ namespace Microsoft.Toolkit.Uwp.UI.Controls
1414
[ContentProperty(Name = nameof(Content))]
1515
public partial class Case : DependencyObject
1616
{
17-
internal SwitchPresenter Parent { get; set; } // TODO: Can we remove Parent need here and just use events?
18-
19-
/// <summary>
20-
/// Event raised when the <see cref="Value"/> property changes.
21-
/// </summary>
22-
public event EventHandler ValueChanged;
23-
2417
/// <summary>
2518
/// Gets or sets the Content to display when this case is active.
2619
/// </summary>
@@ -64,14 +57,7 @@ public object Value
6457
/// Identifies the <see cref="Value"/> property.
6558
/// </summary>
6659
public static readonly DependencyProperty ValueProperty =
67-
DependencyProperty.Register(nameof(Value), typeof(object), typeof(Case), new PropertyMetadata(null, OnValuePropertyChanged));
68-
69-
private static void OnValuePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
70-
{
71-
var xcase = (Case)d;
72-
73-
xcase.ValueChanged?.Invoke(xcase, EventArgs.Empty);
74-
}
60+
DependencyProperty.Register(nameof(Value), typeof(object), typeof(Case), new PropertyMetadata(null));
7561

7662
/// <summary>
7763
/// Initializes a new instance of the <see cref="Case"/> class.

Microsoft.Toolkit.Uwp.UI.Controls.Primitives/SwitchPresenter/CaseCollection.cs

Lines changed: 2 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -5,130 +5,20 @@
55
using System;
66
using System.Collections;
77
using System.Collections.Generic;
8+
using Windows.UI.Xaml;
89

910
namespace Microsoft.Toolkit.Uwp.UI.Controls
1011
{
1112
/// <summary>
1213
/// An collection of <see cref="Case"/> to help with XAML interop.
1314
/// </summary>
14-
public class CaseCollection : IList<Case>, IEnumerable<Case> // TODO: Do we need this or can we use an ObservableCollection directly??? (Or is it useful to have it manage the registration of the child events?)
15+
public class CaseCollection : DependencyObjectCollection
1516
{
16-
internal SwitchPresenter Parent { get; set; } // TODO: Can we remove Parent need here and just use events?
17-
18-
private readonly List<Case> _internalList = new List<Case>();
19-
20-
/// <inheritdoc/>
21-
public int Count => _internalList.Count;
22-
23-
/// <inheritdoc/>
24-
public bool IsReadOnly => false;
25-
26-
/// <inheritdoc/>
27-
public Case this[int index] { get => _internalList[index]; set => Insert(index, value); }
28-
29-
/// <summary>
30-
/// Raised when an animation has been added/removed or modified
31-
/// </summary>
32-
public event EventHandler CaseCollectionChanged;
33-
34-
private void ValueChanged(object sender, EventArgs e)
35-
{
36-
CaseCollectionChanged?.Invoke(this, EventArgs.Empty);
37-
}
38-
3917
/// <summary>
4018
/// Initializes a new instance of the <see cref="CaseCollection"/> class.
4119
/// </summary>
4220
public CaseCollection()
4321
{
4422
}
45-
46-
/// <inheritdoc/>
47-
public int IndexOf(Case item)
48-
{
49-
return _internalList.IndexOf(item);
50-
}
51-
52-
/// <inheritdoc/>
53-
public void Insert(int index, Case item)
54-
{
55-
item.ValueChanged += ValueChanged;
56-
item.Parent = Parent;
57-
_internalList.Insert(index, item);
58-
CaseCollectionChanged?.Invoke(this, EventArgs.Empty);
59-
}
60-
61-
/// <inheritdoc/>
62-
public void RemoveAt(int index)
63-
{
64-
if (index >= 0 && index < _internalList.Count)
65-
{
66-
var xcase = _internalList[index];
67-
xcase.ValueChanged -= ValueChanged;
68-
xcase.Parent = null;
69-
}
70-
71-
_internalList.RemoveAt(index);
72-
CaseCollectionChanged?.Invoke(this, EventArgs.Empty);
73-
}
74-
75-
/// <inheritdoc/>
76-
public void Add(Case item)
77-
{
78-
item.ValueChanged += ValueChanged;
79-
item.Parent = Parent;
80-
_internalList.Add(item);
81-
CaseCollectionChanged?.Invoke(this, EventArgs.Empty);
82-
}
83-
84-
/// <inheritdoc/>
85-
public void Clear()
86-
{
87-
foreach (var xcase in _internalList)
88-
{
89-
xcase.ValueChanged -= ValueChanged;
90-
xcase.Parent = null;
91-
}
92-
93-
_internalList.Clear();
94-
CaseCollectionChanged?.Invoke(this, EventArgs.Empty);
95-
}
96-
97-
/// <inheritdoc/>
98-
public bool Contains(Case item)
99-
{
100-
return _internalList.Contains(item);
101-
}
102-
103-
/// <inheritdoc/>
104-
public void CopyTo(Case[] array, int arrayIndex)
105-
{
106-
_internalList.CopyTo(array, arrayIndex);
107-
}
108-
109-
/// <inheritdoc/>
110-
public bool Remove(Case item)
111-
{
112-
var result = _internalList.Remove(item);
113-
if (result)
114-
{
115-
item.ValueChanged -= ValueChanged;
116-
item.Parent = null;
117-
CaseCollectionChanged?.Invoke(this, EventArgs.Empty);
118-
}
119-
120-
return result;
121-
}
122-
123-
/// <inheritdoc/>
124-
public IEnumerator<Case> GetEnumerator()
125-
{
126-
return _internalList.GetEnumerator();
127-
}
128-
129-
IEnumerator IEnumerable.GetEnumerator()
130-
{
131-
return _internalList.GetEnumerator();
132-
}
13323
}
13424
}

Microsoft.Toolkit.Uwp.UI.Controls.Primitives/SwitchPresenter/SwitchPresenter.cs

Lines changed: 3 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public CaseCollection SwitchCases
4646
/// Indicates the <see cref="SwitchCases"/> property.
4747
/// </summary>
4848
public static readonly DependencyProperty SwitchCasesProperty =
49-
DependencyProperty.Register(nameof(SwitchCases), typeof(CaseCollection), typeof(SwitchPresenter), new PropertyMetadata(null, new PropertyChangedCallback(OnSwitchCasesPropertyChanged)));
49+
DependencyProperty.Register(nameof(SwitchCases), typeof(CaseCollection), typeof(SwitchPresenter), new PropertyMetadata(null));
5050

5151
/// <summary>
5252
/// Gets or sets a value indicating the value to compare all cases against. When this value is bound to and changes, the presenter will automatically evaluate cases and select the new appropriate content from the switch.
@@ -61,7 +61,7 @@ public object Value
6161
/// Indicates the <see cref="Value"/> property.
6262
/// </summary>
6363
public static readonly DependencyProperty ValueProperty =
64-
DependencyProperty.Register(nameof(Value), typeof(object), typeof(SwitchPresenter), new PropertyMetadata(null, new PropertyChangedCallback(OnValuePropertyChanged)));
64+
DependencyProperty.Register(nameof(Value), typeof(object), typeof(SwitchPresenter), new PropertyMetadata(null, OnValuePropertyChanged));
6565

6666
/// <summary>
6767
/// Gets or sets a value indicating which type to first cast and compare provided values against.
@@ -78,26 +78,6 @@ public Type TargetType
7878
public static readonly DependencyProperty TargetTypeProperty =
7979
DependencyProperty.Register(nameof(TargetType), typeof(Type), typeof(SwitchPresenter), new PropertyMetadata(null));
8080

81-
private static void OnSwitchCasesPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
82-
{
83-
if (e.OldValue != null)
84-
{
85-
((SwitchPresenter)e.OldValue).SwitchCases.CaseCollectionChanged -= OnCaseValuePropertyChanged;
86-
}
87-
88-
var xswitch = (SwitchPresenter)d;
89-
90-
foreach (var xcase in xswitch.SwitchCases)
91-
{
92-
// Set our parent
93-
xcase.Parent = xswitch;
94-
}
95-
96-
// Will trigger on collection change and case value changed
97-
xswitch.SwitchCases.Parent = xswitch;
98-
xswitch.SwitchCases.CaseCollectionChanged += OnCaseValuePropertyChanged;
99-
}
100-
10181
private static void OnValuePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
10282
{
10383
// When our Switch's expression changes, re-evaluate.
@@ -106,14 +86,6 @@ private static void OnValuePropertyChanged(DependencyObject d, DependencyPropert
10686
xswitch.EvaluateCases();
10787
}
10888

109-
private static void OnCaseValuePropertyChanged(object sender, EventArgs e)
110-
{
111-
// When something about our collection of cases changes, re-evaluate.
112-
var collection = (CaseCollection)sender;
113-
114-
collection.Parent.EvaluateCases();
115-
}
116-
11789
/// <summary>
11890
/// Initializes a new instance of the <see cref="SwitchPresenter"/> class.
11991
/// </summary>
@@ -144,7 +116,7 @@ private void EvaluateCases()
144116
Case xdefault = null;
145117
Case newcase = null;
146118

147-
foreach (var xcase in SwitchCases)
119+
foreach (Case xcase in SwitchCases)
148120
{
149121
if (xcase.IsDefault)
150122
{

0 commit comments

Comments
 (0)