public void Connect(ICommand command, DependencyObject rootObject, string commandName) { var targetType = DataContext.GetType(); LastCommand = command; var pair = FindCommandPair(targetType, out var ok); var temp = command as RoutedCommand; var binding = SetCommandBinding(rootObject, temp); if (binding == null || !ok || pair == null) { return; } Func <object, ExecutedRoutedEventArgs, Task>?del = null; if (pair.Item1 != null) { del = Delegate.CreateDelegate(typeof(Func <object, ExecutedRoutedEventArgs, Task>), DataContext, pair.Item1, false) as Func <object, ExecutedRoutedEventArgs, Task> ?? new ParameterMapper(pair.Item1, DataContext).Execute; } CanExecuteRoutedEventHandler?del2 = null; if (pair.Item2 != null) { var method = pair.Item2 as MethodInfo; var localTarget = DataContext; if (method == null) { method = MemberInfoHelper.CanExecuteMethod; localTarget = new MemberInfoHelper(pair.Item2, DataContext); } del2 = Delegate.CreateDelegate(typeof(CanExecuteRoutedEventHandler), localTarget, method, false) as CanExecuteRoutedEventHandler ?? new ParameterMapper(method, localTarget).CanExecute; } if (del != null) { _execute = new TaskFactory(del, _isSync).Handler; binding.Executed += _execute; } else { Debug.Print($"CommandBinder: No Compatible method Found: {commandName}"); } if (del2 == null) { return; } _canExecute = del2; binding.CanExecute += del2; }
/// <summary> /// The connect. /// </summary> /// <param name="command"> /// The command. /// </param> /// <param name="targetObject"> /// The target object. /// </param> /// <param name="scheduler"> /// The scheduler. /// </param> /// <param name="commandName"></param> public void Connect([NotNull] ICommand command, [NotNull] DependencyObject targetObject, [NotNull] TaskScheduler scheduler, [NotNull] string commandName) { Contract.Requires<ArgumentNullException>(scheduler != null, "scheduler"); if (Target == null) return; object target = Target.Target; if (target == null) return; Type targetType = target.GetType(); LastCommand = command; bool ok; Tuple<MethodInfo, MemberInfo> pair = FindCommandPair(targetType, out ok); var temp = command as RoutedCommand; CommandBinding binding = SetCommandBinding(targetObject, temp); if (binding == null || !ok || pair == null) return; ExecutedRoutedEventHandler del = null; if (pair.Item1 != null) { del = Delegate.CreateDelegate(typeof (ExecutedRoutedEventHandler), target, pair.Item1, false) .As<ExecutedRoutedEventHandler>() ?? new ParameterMapper(pair.Item1, target).Execute; } CanExecuteRoutedEventHandler del2 = null; if (pair.Item2 != null) { var method = pair.Item2 as MethodInfo; var localTarget = target; if (method == null) { method = MemberInfoHelper.CanExecuteMethod; localTarget = new MemberInfoHelper(pair.Item2, target); } del2 = Delegate.CreateDelegate(typeof (CanExecuteRoutedEventHandler), localTarget, method, false) .As<CanExecuteRoutedEventHandler>() ?? new ParameterMapper(method, localTarget).CanExecute; } if (del != null) binding.Executed += new TaskFactory(del, scheduler, _isSync).Handler; else CommonWpfConstans.LogCommon(false, "CommandBinder: No Compatible method Found: {0}", commandName); if (del2 != null) binding.CanExecute += del2; }