CSLA.NET 5.4.2
CSLA .NET is a software development framework that helps you build a reusable, maintainable object-oriented business layer for your app.
InvokeMethod.cs
Go to the documentation of this file.
1#if !NETFX_CORE && !XAMARIN
2//-----------------------------------------------------------------------
3// <copyright file="InvokeMethod.cs" company="Marimer LLC">
4// Copyright (c) Marimer LLC. All rights reserved.
5// Website: https://cslanet.com
6// </copyright>
7// <summary>Invokes a method on a target object when a </summary>
8//-----------------------------------------------------------------------
9using System;
10using System.Windows;
11using System.Windows.Controls;
12using System.ComponentModel;
13using System.Collections.Generic;
14using Csla.Properties;
15
16namespace Csla.Xaml
17{
23 public class InvokeMethod : FrameworkElement
24 {
25#region Attached properties
26
30 public static readonly DependencyProperty TargetProperty =
31 DependencyProperty.RegisterAttached("Target",
32 typeof(object),
33 typeof(InvokeMethod),
34 new PropertyMetadata(null));
35
41 public static void SetTarget(UIElement ctrl, object value)
42 {
43 ctrl.SetValue(TargetProperty, value);
44 }
45
50 public static object GetTarget(UIElement ctrl)
51 {
52 object result = null;
53 result = ctrl.GetValue(TargetProperty);
54 if (result == null)
55 {
56 var fe = ctrl as FrameworkElement;
57 if (fe != null)
58 result = fe.DataContext;
59 }
60 var icv = result as ICollectionView;
61 if (icv != null)
62 result = icv.CurrentItem;
63 return result;
64 }
65
69 public static readonly DependencyProperty MethodNameProperty =
70 DependencyProperty.RegisterAttached("MethodName",
71 typeof(string),
72 typeof(InvokeMethod),
73 new PropertyMetadata(null));
74
80 public static void SetMethodName(UIElement ctrl, string value)
81 {
82 ctrl.SetValue(MethodNameProperty, value);
83 }
84
89 public static string GetMethodName(UIElement ctrl)
90 {
91 return (string)ctrl.GetValue(MethodNameProperty);
92 }
93
98 public static readonly DependencyProperty TriggerEventProperty =
99 DependencyProperty.RegisterAttached("TriggerEvent",
100 typeof(string),
101 typeof(InvokeMethod),
102 new PropertyMetadata((o, e) =>
103 {
104 var ctrl = o as UIElement;
105 if (ctrl != null)
106 new InvokeMethod(ctrl);
107 }));
108
115 public static void SetTriggerEvent(UIElement ctrl, string value)
116 {
117 ctrl.SetValue(TriggerEventProperty, value);
118 }
119
125 public static string GetTriggerEvent(UIElement ctrl)
126 {
127 return (string)ctrl.GetValue(TriggerEventProperty);
128 }
129
133 public static readonly DependencyProperty MethodParameterProperty =
134 DependencyProperty.RegisterAttached("MethodParameter",
135 typeof(object),
136 typeof(InvokeMethod),
137 new PropertyMetadata(null));
138
144 public static void SetMethodParameter(UIElement ctrl, object value)
145 {
146 ctrl.SetValue(MethodParameterProperty, value);
147 }
148
153 public static object GetMethodParameter(UIElement ctrl)
154 {
155 return ctrl.GetValue(MethodParameterProperty);
156 }
157
158 private static System.Windows.Data.Binding CopyBinding(System.Windows.Data.Binding oldBinding)
159 {
160 var result = new System.Windows.Data.Binding();
161 result.BindsDirectlyToSource = oldBinding.BindsDirectlyToSource;
162 result.Converter = oldBinding.Converter;
163 result.ConverterCulture = oldBinding.ConverterCulture;
164 result.ConverterParameter = oldBinding.ConverterParameter;
165 result.Mode = oldBinding.Mode;
166 result.NotifyOnValidationError = oldBinding.NotifyOnValidationError;
167 result.Path = oldBinding.Path;
168 if (oldBinding.ElementName != null)
169 result.ElementName = oldBinding.ElementName;
170 else if (oldBinding.RelativeSource != null)
171 result.RelativeSource = oldBinding.RelativeSource;
172 else
173 result.Source = oldBinding.Source;
174 result.UpdateSourceTrigger = oldBinding.UpdateSourceTrigger;
175 result.ValidatesOnExceptions = oldBinding.ValidatesOnExceptions;
176 return result;
177 }
178
179#endregion
180
181 private UIElement _element;
182
188 public InvokeMethod(UIElement ctrl)
189 {
190 _element = ctrl;
191 var triggerEvent = GetTriggerEvent(_element);
192 if (!string.IsNullOrEmpty(triggerEvent))
193 {
194 // hook up the trigger event
195 var eventRef = ctrl.GetType().GetEvent(triggerEvent);
196 if (eventRef != null)
197 {
198 var invoke = eventRef.EventHandlerType.GetMethod("Invoke");
199 var p = invoke.GetParameters();
200 if (p.Length == 2)
201 {
202 var p1Type = p[1].ParameterType;
203 if (typeof(EventArgs).IsAssignableFrom(p1Type))
204 {
205 var del = Delegate.CreateDelegate(eventRef.EventHandlerType,
206 this,
207 this.GetType().GetMethod("CallMethod", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic));
208 eventRef.AddEventHandler(ctrl, del);
209 }
210 else
211 {
212 throw new NotSupportedException(Csla.Properties.Resources.ExecuteBadTriggerEvent);
213 }
214 }
215 else
216 throw new NotSupportedException(Csla.Properties.Resources.ExecuteBadTriggerEvent);
217 }
218 }
219 }
220
221 private void CallMethod(object sender, EventArgs e)
222 {
223 object target = GetTarget(_element);
224 var methodName = GetMethodName(_element);
225 var targetMethod = target.GetType().GetMethod(methodName);
226 if (targetMethod == null)
227 throw new MissingMethodException(methodName);
228
229 object p = GetMethodParameter(_element);
230 var pCount = targetMethod.GetParameters().Length;
231 try
232 {
233 if (pCount == 0)
234 targetMethod.Invoke(target, null);
235 else if (pCount == 2)
236 targetMethod.Invoke(target, new object[] { this, new ExecuteEventArgs
237 {
238 MethodParameter = p,
239 TriggerParameter = e,
240 TriggerSource = (FrameworkElement)_element
241 }});
242 else
243 throw new NotSupportedException(Csla.Properties.Resources.ExecuteBadParams);
244 }
245 catch (System.Reflection.TargetInvocationException ex)
246 {
247 if (ex.InnerException != null)
248 throw ex.InnerException;
249 else
250 throw;
251 }
252 }
253 }
254}
255#endif
A strongly-typed resource class, for looking up localized strings, etc.
static string ExecuteBadParams
Looks up a localized string similar to Method to be executed must have 0 or 2 parameters.
static string ExecuteBadTriggerEvent
Looks up a localized string similar to Trigger event has an unsupported signature.
Arguments passed to a method invoked by the Execute trigger action.
Invokes a method on a target object when a trigger event is raised from the attached UI control.
Definition: InvokeMethod.cs:24
InvokeMethod(UIElement ctrl)
Invokes the target method if all required attached property values have been set.
static readonly DependencyProperty TriggerEventProperty
Name of event raised by UI control that triggers invoking the target method.
Definition: InvokeMethod.cs:98
static void SetMethodParameter(UIElement ctrl, object value)
Sets the parameter value to be passed to invoked method.
static void SetTriggerEvent(UIElement ctrl, string value)
Sets the name of event raised by UI control that triggers invoking the target method.
static void SetMethodName(UIElement ctrl, string value)
Sets the name of method to be invoked.
Definition: InvokeMethod.cs:80
static void SetTarget(UIElement ctrl, object value)
Sets the object containing the method to be invoked.
Definition: InvokeMethod.cs:41
static string GetMethodName(UIElement ctrl)
Gets the name of method to be invoked.
Definition: InvokeMethod.cs:89
static object GetTarget(UIElement ctrl)
Gets the object containing the method to be invoked.
Definition: InvokeMethod.cs:50
static readonly DependencyProperty TargetProperty
Object containing the method to be invoked.
Definition: InvokeMethod.cs:30
static string GetTriggerEvent(UIElement ctrl)
Gets the name of event raised by UI control that triggers invoking the target method.
static object GetMethodParameter(UIElement ctrl)
Gets the parameter value to be passed to invoked method.
static readonly DependencyProperty MethodParameterProperty
Parameter value to be passed to invoked method.
static readonly DependencyProperty MethodNameProperty
Name of method to be invoked.
Definition: InvokeMethod.cs:69