private static MethodInfo GetInvokeMethod(CommandBehaviorBinding behavior) { var owner = ((FrameworkElement)behavior.Owner); var dataContext = ((FrameworkElement)behavior.Owner).DataContext; object obj = behavior.InvokeObject is Type ? null : (behavior.InvokeObject == null ? dataContext : null); Type type = behavior.InvokeObject is Type ? (Type)behavior.InvokeObject : (behavior.InvokeObject != null ? behavior.InvokeObject.GetType() : dataContext.GetType()); var flags = behavior.InvokeObject is Type ? BindingFlags.Static : BindingFlags.Instance; var key = behavior.InvokeMethodName + "(object); " + type.AssemblyQualifiedName; if (StaticMethodInfoCache.ContainsKey(key)) { return(StaticMethodInfoCache[key]); } var method = type.GetMethod(behavior.InvokeMethodName, flags | BindingFlags.Public, null, new[] { typeof(object) }, null); if (method != null) { StaticMethodInfoCache.Add(key, method); return(method); } #region Search private method. need full trust! method = type.GetMethod(behavior.InvokeMethodName, flags | BindingFlags.NonPublic, null, new[] { typeof(object) }, null); if (method != null) { Debug.WriteLine("=>" + string.Format("KsWare.AttachedBehavior Warning: '{0}(object)' method should be public on '{3}' ''{1}' (HashCode={2})'", behavior.InvokeMethodName, type.Name, (obj != null ? obj.GetHashCode().ToString() : ""), obj != null ? "object":"type" )); StaticMethodInfoCache.Add(key, method); return(method); } #endregion Debug.WriteLine("=>" + ($"KsWare.AttachedBehavior Error: public '{behavior.InvokeMethodName}(object)' method not found on 'object' ''{owner.DataContext.GetType().Name}' (HashCode={owner.DataContext.GetHashCode()})'" + $"; target element is '{owner.GetType().Name}' (Name='{owner.Name}')" + "; target property is 'Invoke' (type 'String')")); //throw new MissingMethodException( ((FrameworkElement) d).DataContext.GetType().Name,methodName); StaticMethodInfoCache.Add(key, null); return(null); }
/// <summary> Handles changes to the Action property. /// </summary> private static void OnActionChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { CommandBehaviorBinding binding = FetchOrCreateBinding(d); binding.Action = (Action <object>)e.NewValue; }
/// <summary> Handles changes to the Command property. /// </summary> private static void OnCommandChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { CommandBehaviorBinding binding = FetchOrCreateBinding(d); binding.Command = (ICommand)e.NewValue; }
/// <summary> Handles changes to the InvokeMethod property. /// </summary> private static void OnInvokeMethodChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { CommandBehaviorBinding binding = FetchOrCreateBinding(d); binding.InvokeMethodName = (string)e.NewValue; }