From 17fdde66d94155fc62a034fa6658995bef6fd6e5 Mon Sep 17 00:00:00 2001 From: Jason Smith Date: Tue, 22 Mar 2016 13:02:25 -0700 Subject: Initial import --- Xamarin.Forms.Core/Interactivity/Behavior.cs | 65 ++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 Xamarin.Forms.Core/Interactivity/Behavior.cs (limited to 'Xamarin.Forms.Core/Interactivity/Behavior.cs') diff --git a/Xamarin.Forms.Core/Interactivity/Behavior.cs b/Xamarin.Forms.Core/Interactivity/Behavior.cs new file mode 100644 index 00000000..f8689041 --- /dev/null +++ b/Xamarin.Forms.Core/Interactivity/Behavior.cs @@ -0,0 +1,65 @@ +using System; + +namespace Xamarin.Forms +{ + public abstract class Behavior : BindableObject, IAttachedObject + { + internal Behavior(Type associatedType) + { + if (associatedType == null) + throw new ArgumentNullException("associatedType"); + AssociatedType = associatedType; + } + + protected Type AssociatedType { get; } + + void IAttachedObject.AttachTo(BindableObject bindable) + { + if (bindable == null) + throw new ArgumentNullException("bindable"); + if (!AssociatedType.IsInstanceOfType(bindable)) + throw new InvalidOperationException("bindable not an instance of AssociatedType"); + OnAttachedTo(bindable); + } + + void IAttachedObject.DetachFrom(BindableObject bindable) + { + OnDetachingFrom(bindable); + } + + protected virtual void OnAttachedTo(BindableObject bindable) + { + } + + protected virtual void OnDetachingFrom(BindableObject bindable) + { + } + } + + public abstract class Behavior : Behavior where T : BindableObject + { + protected Behavior() : base(typeof(T)) + { + } + + protected override void OnAttachedTo(BindableObject bindable) + { + base.OnAttachedTo(bindable); + OnAttachedTo((T)bindable); + } + + protected virtual void OnAttachedTo(T bindable) + { + } + + protected override void OnDetachingFrom(BindableObject bindable) + { + OnDetachingFrom((T)bindable); + base.OnDetachingFrom(bindable); + } + + protected virtual void OnDetachingFrom(T bindable) + { + } + } +} \ No newline at end of file -- cgit v1.2.3