summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Core/Interactivity/Behavior.cs
blob: f8689041a803e5eb9e8d82dffd02b759a0828a06 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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<T> : 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)
		{
		}
	}
}