Skip to content

Commit e555b88

Browse files
authored
Merge pull request #986 from Caliburn-Micro/alert-fix-72
Refactor AttachedCollection for readability and clarity
2 parents 5c3bfb2 + 8f385db commit e555b88

File tree

2 files changed

+36
-26
lines changed

2 files changed

+36
-26
lines changed

src/Caliburn.Micro.Platform/Platforms/Maui/AttachedCollection.cs

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
namespace Caliburn.Micro.Maui
1+
namespace Caliburn.Micro.Maui
22
{
3-
using System;
43
using System.Collections.Specialized;
54
using System.Linq;
65
using global::Microsoft.Maui.Controls;
@@ -10,7 +9,7 @@
109
/// </summary>
1110
/// <typeparam name="T">The type of item in the attached collection.</typeparam>
1211
public class AttachedCollection<T> : BindableCollection<BindableObject>, IAttachedObject
13-
where T : BindableObject, IAttachedObject
12+
where T : BindableObject, IAttachedObject
1413
{
1514
private BindableObject associatedObject;
1615

@@ -27,45 +26,51 @@ public void Attach(BindableObject dependencyObject)
2726
/// <summary>
2827
/// Detaches the collection.
2928
/// </summary>
30-
public void Detach() {
29+
public void Detach()
30+
{
3131
this.OfType<IAttachedObject>().Apply(x => x.Detach());
3232
associatedObject = null;
3333
}
3434

3535
/// <summary>
3636
/// The currently attached object.
3737
/// </summary>
38-
public BindableObject AssociatedObject {
38+
public BindableObject AssociatedObject
39+
{
3940
get { return associatedObject; }
4041
}
4142

4243
/// <summary>
4344
/// Called when an item is added from the collection.
4445
/// </summary>
4546
/// <param name="item">The item that was added.</param>
46-
protected virtual void OnItemAdded(BindableObject item) {
47-
if (associatedObject != null) {
48-
if (item is IAttachedObject)
49-
((IAttachedObject) item).Attach(associatedObject);
47+
protected virtual void OnItemAdded(BindableObject item)
48+
{
49+
if (associatedObject != null && item is IAttachedObject attached)
50+
{
51+
attached.Attach(associatedObject);
5052
}
5153
}
5254

5355
/// <summary>
5456
/// Called when an item is removed from the collection.
5557
/// </summary>
5658
/// <param name="item">The item that was removed.</param>
57-
protected virtual void OnItemRemoved(BindableObject item) {
58-
if (item is IAttachedObject) {
59-
if (((IAttachedObject) item).AssociatedObject != null)
60-
((IAttachedObject) item).Detach();
59+
protected virtual void OnItemRemoved(BindableObject item)
60+
{
61+
if (item is IAttachedObject attached &&
62+
attached.AssociatedObject != null)
63+
{
64+
attached.Detach();
6165
}
6266
}
6367

6468
/// <summary>
6569
/// Raises the <see cref = "E:System.Collections.ObjectModel.ObservableCollection`1.CollectionChanged" /> event with the provided arguments.
6670
/// </summary>
6771
/// <param name = "e">Arguments of the event being raised.</param>
68-
protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e) {
72+
protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
73+
{
6974
base.OnCollectionChanged(e);
7075

7176
if (e.OldItems != null)

src/Caliburn.Micro.Platform/Platforms/Xamarin.Forms/AttachedCollection.cs

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
namespace Caliburn.Micro.Xamarin.Forms
22
{
3-
using System;
43
using System.Collections.Specialized;
54
using System.Linq;
65
using global::Xamarin.Forms;
@@ -10,7 +9,7 @@
109
/// </summary>
1110
/// <typeparam name="T">The type of item in the attached collection.</typeparam>
1211
public class AttachedCollection<T> : BindableCollection<BindableObject>, IAttachedObject
13-
where T : BindableObject, IAttachedObject
12+
where T : BindableObject, IAttachedObject
1413
{
1514
private BindableObject associatedObject;
1615

@@ -27,44 +26,50 @@ public void Attach(BindableObject dependencyObject)
2726
/// <summary>
2827
/// Detaches the collection.
2928
/// </summary>
30-
public void Detach() {
29+
public void Detach()
30+
{
3131
this.OfType<IAttachedObject>().Apply(x => x.Detach());
3232
associatedObject = null;
3333
}
3434

3535
/// <summary>
3636
/// The currently attached object.
3737
/// </summary>
38-
public BindableObject AssociatedObject {
38+
public BindableObject AssociatedObject
39+
{
3940
get { return associatedObject; }
4041
}
4142

4243
/// <summary>
4344
/// Called when an item is added from the collection.
4445
/// </summary>
4546
/// <param name="item">The item that was added.</param>
46-
protected virtual void OnItemAdded(BindableObject item) {
47-
if (associatedObject != null) {
48-
if (item is IAttachedObject)
49-
((IAttachedObject) item).Attach(associatedObject);
47+
protected virtual void OnItemAdded(BindableObject item)
48+
{
49+
if (associatedObject != null && item is IAttachedObject attached)
50+
{
51+
attached.Attach(associatedObject);
5052
}
5153
}
5254

5355
/// <summary>
5456
/// Called when an item is removed from the collection.
5557
/// </summary>
5658
/// <param name="item">The item that was removed.</param>
57-
protected virtual void OnItemRemoved(BindableObject item) {
58-
if (item is IAttachedObject && ((IAttachedObject) item).AssociatedObject != null) {
59-
((IAttachedObject) item).Detach();
59+
protected virtual void OnItemRemoved(BindableObject item)
60+
{
61+
if (item is IAttachedObject attached && attached.AssociatedObject != null)
62+
{
63+
attached.Detach();
6064
}
6165
}
6266

6367
/// <summary>
6468
/// Raises the <see cref = "E:System.Collections.ObjectModel.ObservableCollection`1.CollectionChanged" /> event with the provided arguments.
6569
/// </summary>
6670
/// <param name = "e">Arguments of the event being raised.</param>
67-
protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e) {
71+
protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
72+
{
6873
base.OnCollectionChanged(e);
6974

7075
if (e.OldItems != null)

0 commit comments

Comments
 (0)