388f3a8377cdef3c6614e0690197b165b8ffcc6d
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / internal / XamlBinding / Interactivity / Behavior.cs
1 using System;
2 using System.Reflection;
3
4 namespace Tizen.NUI.Binding
5 {
6     internal abstract class Behavior : BindableObject, IAttachedObject
7     {
8         internal Behavior(Type associatedType)
9         {
10             if (associatedType == null)
11                 throw new ArgumentNullException("associatedType");
12             AssociatedType = associatedType;
13         }
14
15         protected Type AssociatedType { get; }
16
17         void IAttachedObject.AttachTo(BindableObject bindable)
18         {
19             if (bindable == null)
20                 throw new ArgumentNullException("bindable");
21             if (!AssociatedType.IsInstanceOfType(bindable))
22                 throw new InvalidOperationException("bindable not an instance of AssociatedType");
23             OnAttachedTo(bindable);
24         }
25
26         void IAttachedObject.DetachFrom(BindableObject bindable)
27         {
28             OnDetachingFrom(bindable);
29         }
30
31         protected virtual void OnAttachedTo(BindableObject bindable)
32         {
33         }
34
35         protected virtual void OnDetachingFrom(BindableObject bindable)
36         {
37         }
38     }
39
40     internal abstract class Behavior<T> : Behavior where T : BindableObject
41     {
42         protected Behavior() : base(typeof(T))
43         {
44         }
45
46         protected override void OnAttachedTo(BindableObject bindable)
47         {
48             base.OnAttachedTo(bindable);
49             OnAttachedTo((T)bindable);
50         }
51
52         protected virtual void OnAttachedTo(T bindable)
53         {
54         }
55
56         protected override void OnDetachingFrom(BindableObject bindable)
57         {
58             OnDetachingFrom((T)bindable);
59             base.OnDetachingFrom(bindable);
60         }
61
62         protected virtual void OnDetachingFrom(T bindable)
63         {
64         }
65     }
66 }