/// <summary> /// Creates a commands binding from event and a property /// </summary> /// <returns>The binding from event.</returns> /// <param name="command">Command.</param> /// <param name="target">Target.</param> /// <param name="commandParameter">Command parameter.</param> /// <param name="eventName">Event name.</param> /// <param name="enablePropertyName">Enable property name.</param> protected static IDisposable ForEvent(ICommand command, object target, IObservable <object> commandParameter, string eventName, PropertyInfo enabledProperty) { commandParameter = commandParameter ?? Observable.Return(target); object latestParam = null; var ctl = target; var actionDisp = Observable.FromEventPattern(ctl, eventName).Subscribe((e) => { if (command.CanExecute(latestParam)) { command.Execute(latestParam); } }); var enabledSetter = Reflection.GetValueSetterForProperty(enabledProperty); if (enabledSetter == null) { return(actionDisp); } // initial enabled state enabledSetter(target, command.CanExecute(latestParam), null); var compDisp = new CompositeDisposable( actionDisp, commandParameter.Subscribe(x => latestParam = x), Observable.FromEventPattern <EventHandler, EventArgs>(x => command.CanExecuteChanged += x, x => command.CanExecuteChanged -= x) .Select(_ => command.CanExecute(latestParam)) .Subscribe(x => enabledSetter(target, x, null))); return(compDisp); }
/// <inheritdoc/> public IDisposable BindCommandToObject(ICommand command, object target, IObservable <object> commandParameter) { if (command == null) { throw new ArgumentNullException(nameof(command)); } if (target == null) { throw new ArgumentNullException(nameof(target)); } commandParameter = commandParameter ?? Observable.Return(target); object latestParam = null; var ctlDelegate = new ControlDelegate( x => { if (command.CanExecute(latestParam)) { command.Execute(latestParam); } }) { IsEnabled = command.CanExecute(latestParam) }; var sel = new Selector("theAction:"); // TODO how does this work? Is there an Action property? Reflection.GetValueSetterOrThrow(target.GetType().GetRuntimeProperty("Action"))(target, sel, null); var targetSetter = Reflection.GetValueSetterOrThrow(target.GetType().GetRuntimeProperty("Target")); targetSetter(target, ctlDelegate, null); var actionDisp = Disposable.Create(() => targetSetter(target, null, null)); var enabledSetter = Reflection.GetValueSetterForProperty(target.GetType().GetRuntimeProperty("Enabled")); if (enabledSetter == null) { return(actionDisp); } // initial enabled state enabledSetter(target, command.CanExecute(latestParam), null); var compDisp = new CompositeDisposable( actionDisp, commandParameter.Subscribe(x => latestParam = x), Observable.FromEventPattern <EventHandler, EventArgs>(x => command.CanExecuteChanged += x, x => command.CanExecuteChanged -= x) .Select(_ => command.CanExecute(latestParam)) .Subscribe(x => { enabledSetter(target, x, null); ctlDelegate.IsEnabled = x; })); return(compDisp); }
/// <summary> /// Creates a commands binding from event and a property. /// </summary> /// <param name="command">The command.</param> /// <param name="target">The target object.</param> /// <param name="commandParameter">The command parameter.</param> /// <param name="enabledProperty">The enabled property.</param> /// <returns>Returns a disposable.</returns> protected static IDisposable ForTargetAction(ICommand command, object target, IObservable <object> commandParameter, PropertyInfo enabledProperty) { if (command == null) { throw new ArgumentNullException(nameof(command)); } commandParameter = commandParameter ?? Observable.Return(target); object latestParam = null; IDisposable actionDisp = null; var ctl = target as UIControl; if (ctl != null) { var eh = new EventHandler((o, e) => { if (command.CanExecute(latestParam)) { command.Execute(latestParam); } }); ctl.AddTarget(eh, UIControlEvent.TouchUpInside); actionDisp = Disposable.Create(() => ctl.RemoveTarget(eh, UIControlEvent.TouchUpInside)); } var enabledSetter = Reflection.GetValueSetterForProperty(enabledProperty); if (enabledSetter == null) { return(actionDisp); } // Initial enabled state enabledSetter(target, command.CanExecute(latestParam), null); return(new CompositeDisposable( actionDisp, commandParameter.Subscribe(x => latestParam = x), Observable.FromEvent <EventHandler, bool>( eventHandler => { void Handler(object sender, EventArgs e) => eventHandler(command.CanExecute(latestParam)); return Handler; }, x => command.CanExecuteChanged += x, x => command.CanExecuteChanged -= x) .Subscribe(x => enabledSetter(target, x, null)))); }
protected static IDisposable ForEvent(ICommand command, object target, IObservable <object> commandParameter, string eventName, PropertyInfo enabledProperty) { if (command == null) { throw new ArgumentNullException(nameof(command)); } commandParameter = commandParameter ?? Observable.Return(target); object?latestParam = null; var ctl = target; var actionDisp = Observable.FromEventPattern(ctl, eventName).Subscribe(_ => { if (command.CanExecute(latestParam)) { command.Execute(latestParam); } }); Action <object, object?, object[]?>?enabledSetter = Reflection.GetValueSetterForProperty(enabledProperty); if (enabledSetter == null) { return(actionDisp); } // initial enabled state enabledSetter(target, command.CanExecute(latestParam), null); var compDisp = new CompositeDisposable( actionDisp, commandParameter.Subscribe(x => latestParam = x), Observable.FromEvent <EventHandler, bool>( eventHandler => { void Handler(object sender, EventArgs e) => eventHandler(command.CanExecute(latestParam)); return(Handler); }, x => command.CanExecuteChanged += x, x => command.CanExecuteChanged -= x) .Subscribe(x => enabledSetter(target, x, null))); return(compDisp); }