1: public class EventToCommand : Behavior<FrameworkElement>
2: {
3: #region EventName
4:
5: public static readonly DependencyProperty EventNameProperty =
6: DependencyProperty.Register(
7: "EventName",
8: typeof(string),
9: typeof(EventToCommand),
10: new PropertyMetadata(null));
11:
12: public string EventName
13: {
14: get { return (string)GetValue(EventNameProperty); }
15: set { SetValue(EventNameProperty, value); }
16: }
17:
18: #endregion
19:
20: #region Command
21:
22: public static readonly DependencyProperty CommandProperty =
23: DependencyProperty.Register(
24: "Command",
25: typeof(ICommand),
26: typeof(EventToCommand),
27: new PropertyMetadata(null));
28:
29: public ICommand Command
30: {
31: get { return (ICommand)GetValue(CommandProperty); }
32: set { SetValue(CommandProperty, value); }
33: }
34:
35: #endregion
36:
37: #region CommandParameter
38:
39: public static readonly DependencyProperty CommandParameterProperty =
40: DependencyProperty.Register(
41: "CommandParameter",
42: typeof(object),
43: typeof(EventToCommand),
44: new PropertyMetadata(null));
45:
46: public object CommandParameter
47: {
48: get { return (object)GetValue(CommandParameterProperty); }
49: set { SetValue(CommandParameterProperty, value); }
50: }
51:
52: #endregion
53:
54: #region UseEventArgs
55:
56: public static readonly DependencyProperty UseEventArgsProperty =
57: DependencyProperty.Register(
58: "UseEventArgs",
59: typeof(bool),
60: typeof(EventToCommand),
61: new PropertyMetadata(null));
62:
63: public bool UseEventArgs
64: {
65: get { return (bool)GetValue(UseEventArgsProperty); }
66: set { SetValue(UseEventArgsProperty, value); }
67: }
68:
69: #endregion
70:
71: #region Handlers
72:
73: protected override void OnAttached()
74: {
75: if (this.AssociatedObject != null &&
76: !string.IsNullOrEmpty(this.EventName))
77: {
78: EventInfo eventInfo = this.GetEventInfo(this.AssociatedObject.GetType(), this.EventName);
79:
80: if (eventInfo != null)
81: {
82: Delegate handler = this.GetEventHandler(eventInfo);
83:
84: WindowsRuntimeMarshal.AddEventHandler<Delegate>(
85: dlg => (EventRegistrationToken)eventInfo.AddMethod.Invoke(this.AssociatedObject, new object[] { dlg }),
86: etr => eventInfo.RemoveMethod.Invoke(this.AssociatedObject, new object[] { etr }), handler);
87: }
88: }
89:
90: base.OnAttached();
91: }
92:
93: protected override void OnDetaching()
94: {
95: if (this.AssociatedObject != null &&
96: !string.IsNullOrEmpty(this.EventName))
97: {
98: EventInfo eventInfo = this.GetEventInfo(this.AssociatedObject.GetType(), this.EventName);
99:
100: if (eventInfo != null)
101: {
102: Delegate handler = this.GetEventHandler(eventInfo);
103:
104: WindowsRuntimeMarshal.RemoveEventHandler<Delegate>(
105: etr => eventInfo.RemoveMethod.Invoke(this.AssociatedObject, new object[] { etr }), handler);
106: }
107: }
108:
109: base.OnDetaching();
110: }
111:
112: #endregion
113:
114: #region Event Management
115:
116: private EventInfo GetEventInfo(Type type, string eventName)
117: {
118: EventInfo eventInfo = type.GetTypeInfo().GetDeclaredEvent(eventName);
119:
120: if (eventInfo == null)
121: {
122: Type baseType = type.GetTypeInfo().BaseType;
123:
124: if (baseType != null)
125: return GetEventInfo(type.GetTypeInfo().BaseType, eventName);
126: else
127: return eventInfo;
128: }
129:
130: return eventInfo;
131: }
132:
133: /// <summary>
134: /// Create an instance of a delegate able to handle the provided event
135: /// </summary>
136: /// <param name="eventInfo">Information about the event to create the delegate</param>
137: /// <returns></returns>
138: public Delegate GetEventHandler(EventInfo eventInfo)
139: {
140: Delegate dlg = null;
141:
142: if (eventInfo == null)
143: throw new ArgumentNullException("eventInfo");
144:
145: if (eventInfo.EventHandlerType == null)
146: throw new ArgumentNullException("eventInfo.EventHandlerType");
147:
148: if (dlg == null)
149: dlg = this.GetType()
150: .GetTypeInfo()
151: .GetDeclaredMethod("OnEventRaised")
152: .CreateDelegate(eventInfo.EventHandlerType, this);
153:
154: return dlg;
155: }
156:
157: /// <summary>
158: /// Received the notifications when the wrapped event has been raised.
159: /// </summary>
160: /// <param name="sender">source of the event</param>
161: /// <param name="e">arguments of the event</param>
162: private void OnEventRaised(object sender, object e)
163: {
164: if (this.Command != null)
165: {
166: if (this.UseEventArgs)
167: this.Command.Execute(e);
168: else
169: this.Command.Execute(this.CommandParameter);
170: }
171: }
172:
173: #endregion
174: }