{
readonly List<WeakReference> _associatedObjects = new List<WeakReference>();
+ const int CleanupTrigger = 128;
+ int _cleanupThreshold = CleanupTrigger;
+
public AttachedCollection()
{
}
lock (_associatedObjects)
{
_associatedObjects.Add(new WeakReference(bindable));
+ CleanUpWeakReferences();
}
foreach (T item in this)
item.AttachTo(bindable);
item.AttachTo(bindable);
}
}
+
+ void CleanUpWeakReferences()
+ {
+ if (_associatedObjects.Count < _cleanupThreshold)
+ {
+ return;
+ }
+
+ _associatedObjects.RemoveAll(t => !t.IsAlive);
+ _cleanupThreshold = _associatedObjects.Count + CleanupTrigger;
+ }
}
}
\ No newline at end of file
{
internal const string StyleClassPrefix = "Xamarin.Forms.StyleClass.";
+ const int CleanupTrigger = 128;
+ int _cleanupThreshold = CleanupTrigger;
+
readonly BindableProperty _basedOnResourceProperty = BindableProperty.CreateAttached("BasedOnResource", typeof(Style), typeof(Style), default(Style),
propertyChanged: OnBasedOnResourceChanged);
if (BaseResourceKey != null)
bindable.SetDynamicResource(_basedOnResourceProperty, BaseResourceKey);
ApplyCore(bindable, BasedOn ?? GetBasedOnResource(bindable));
+
+ CleanUpWeakReferences();
}
public Type TargetType { get; }
return true;
return value.TargetType.IsAssignableFrom(TargetType);
}
+
+ void CleanUpWeakReferences()
+ {
+ if (_targets.Count < _cleanupThreshold)
+ {
+ return;
+ }
+
+ _targets.RemoveAll(t => !t.TryGetTarget(out _));
+ _cleanupThreshold = _targets.Count + CleanupTrigger;
+ }
}
}
\ No newline at end of file