2 using System.Collections.Generic;
3 using System.ComponentModel;
5 using System.Reflection;
6 using Tizen.NUI.XamlBinding.Internals;
8 namespace Tizen.NUI.XamlBinding
11 /// The class EventTrigger.
13 /// <since_tizen> 6 </since_tizen>
14 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
15 [EditorBrowsable(EditorBrowsableState.Never)]
16 [ContentProperty("Actions")]
17 public sealed class EventTrigger : TriggerBase
19 static readonly MethodInfo s_handlerinfo = typeof(EventTrigger).GetRuntimeMethods().Single(mi => mi.Name == "OnEventTriggered" && mi.IsPublic == false);
20 readonly List<BindableObject> _associatedObjects = new List<BindableObject>();
25 Delegate _handlerdelegate;
27 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
28 [EditorBrowsable(EditorBrowsableState.Never)]
29 public EventTrigger() : base(typeof(BindableObject))
31 Actions = new SealedList<TriggerAction>();
34 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
35 [EditorBrowsable(EditorBrowsableState.Never)]
36 public IList<TriggerAction> Actions { get; }
38 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
39 [EditorBrowsable(EditorBrowsableState.Never)]
42 get { return _eventname; }
45 if (_eventname == value)
48 throw new InvalidOperationException("Event cannot be changed once the Trigger has been applied");
55 internal override void OnAttachedTo(BindableObject bindable)
57 base.OnAttachedTo(bindable);
58 if (!string.IsNullOrEmpty(Event))
59 AttachHandlerTo(bindable);
60 _associatedObjects.Add(bindable);
63 internal override void OnDetachingFrom(BindableObject bindable)
65 _associatedObjects.Remove(bindable);
66 DetachHandlerFrom(bindable);
67 base.OnDetachingFrom(bindable);
70 internal override void OnSeal()
73 ((SealedList<TriggerAction>)Actions).IsReadOnly = true;
76 void AttachHandlerTo(BindableObject bindable)
80 _eventinfo = bindable.GetType().GetRuntimeEvent(Event);
81 _handlerdelegate = s_handlerinfo.CreateDelegate(_eventinfo?.EventHandlerType, this);
85 Console.WriteLine("EventTrigger", "Can not attach EventTrigger to {0}.{1}. Check if the handler exists and if the signature is right.", bindable.GetType(), Event);
87 if (_eventinfo != null && _handlerdelegate != null)
88 _eventinfo.AddEventHandler(bindable, _handlerdelegate);
91 void DetachHandlerFrom(BindableObject bindable)
93 if (_eventinfo != null && _handlerdelegate != null)
94 _eventinfo.RemoveEventHandler(bindable, _handlerdelegate);
98 void OnEventTriggered(object sender, EventArgs e)
100 var bindable = (BindableObject)sender;
101 foreach (TriggerAction action in Actions)
102 action.DoInvoke(bindable);