2 using System.Collections.Generic;
3 using System.ComponentModel;
5 using System.Reflection;
6 using Tizen.NUI.Binding.Internals;
8 namespace Tizen.NUI.Binding
10 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
11 [EditorBrowsable(EditorBrowsableState.Never)]
12 [ContentProperty("Actions")]
13 public sealed class EventTrigger : TriggerBase
15 static readonly MethodInfo s_handlerinfo = typeof(EventTrigger).GetRuntimeMethods().Single(mi => mi.Name == "OnEventTriggered" && mi.IsPublic == false);
16 readonly List<BindableObject> _associatedObjects = new List<BindableObject>();
21 Delegate _handlerdelegate;
23 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
24 [EditorBrowsable(EditorBrowsableState.Never)]
25 public EventTrigger() : base(typeof(BindableObject))
27 Actions = new SealedList<TriggerAction>();
30 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
31 [EditorBrowsable(EditorBrowsableState.Never)]
32 public IList<TriggerAction> Actions { get; }
34 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
35 [EditorBrowsable(EditorBrowsableState.Never)]
38 get { return _eventname; }
41 if (_eventname == value)
44 throw new InvalidOperationException("Event cannot be changed once the Trigger has been applied");
51 internal override void OnAttachedTo(BindableObject bindable)
53 base.OnAttachedTo(bindable);
54 if (!string.IsNullOrEmpty(Event))
55 AttachHandlerTo(bindable);
56 _associatedObjects.Add(bindable);
59 internal override void OnDetachingFrom(BindableObject bindable)
61 _associatedObjects.Remove(bindable);
62 DetachHandlerFrom(bindable);
63 base.OnDetachingFrom(bindable);
66 internal override void OnSeal()
69 ((SealedList<TriggerAction>)Actions).IsReadOnly = true;
72 void AttachHandlerTo(BindableObject bindable)
76 _eventinfo = bindable.GetType().GetRuntimeEvent(Event);
77 _handlerdelegate = s_handlerinfo.CreateDelegate(_eventinfo?.EventHandlerType, this);
81 Console.WriteLine($"EventTrigger", "Can not attach EventTrigger to {binding.GetType()}.{Evnet}. Check if the handler exists and if the signature is right.");
83 if (_eventinfo != null && _handlerdelegate != null)
84 _eventinfo.AddEventHandler(bindable, _handlerdelegate);
87 void DetachHandlerFrom(BindableObject bindable)
89 if (_eventinfo != null && _handlerdelegate != null)
90 _eventinfo.RemoveEventHandler(bindable, _handlerdelegate);
94 void OnEventTriggered(object sender, EventArgs e)
96 var bindable = (BindableObject)sender;
97 foreach (TriggerAction action in Actions)
98 action.DoInvoke(bindable);