Example #1
0
        IDisposable bindCommandInternal <TView, TViewModel, TProp, TParam>(
            TViewModel viewModel,
            TView view,
            Expression <Func <TViewModel, TProp> > propertyName,
            Func <object, object> viewPropGetter,
            IObservable <TParam> withParameter,
            string toEvent,
            out IObservable <TProp> changed,
            Func <ICommand, ICommand> commandFixuper = null)
            where TViewModel : class
            where TView : class, IViewFor <TViewModel>
            where TProp : ICommand
        {
            var vmPropChain = Reflection.ExpressionToPropertyNames(propertyName);

            IDisposable disp = Disposable.Empty;

            changed = Reflection.ViewModelWhenAnyValue(viewModel, view, propertyName).Publish().RefCount();

            var propSub = changed.Subscribe(x => {
                disp.Dispose();
                if (x == null)
                {
                    disp = Disposable.Empty;
                    return;
                }

                var vmString = String.Join(".", vmPropChain);

                var target = viewPropGetter(view);
                if (target == null)
                {
                    this.Log().Error("Binding {0}.{1} => {2}.{1} failed because target is null",
                                     typeof(TViewModel).FullName, vmString, view.GetType().FullName);
                    disp = Disposable.Empty;
                }

                var cmd = commandFixuper != null ? commandFixuper(x) : x;
                if (toEvent != null)
                {
                    disp = CreatesCommandBinding.BindCommandToObject(cmd, target, withParameter.Select(y => (object)y), toEvent);
                }
                else
                {
                    disp = CreatesCommandBinding.BindCommandToObject(cmd, target, withParameter.Select(y => (object)y));
                }
            });

            return(Disposable.Create(() => {
                propSub.Dispose();
                disp.Dispose();
            }));
        }
        private IDisposable BindCommandInternal <TView, TProp, TParam>(
            IObservable <TProp> @this,
            TView view,
            Expression controlExpression,
            IObservable <TParam> withParameter,
            string toEvent,
            Func <ICommand, ICommand> commandFixuper = null)
            where TView : class, IViewFor
            where TProp : ICommand
        {
            IDisposable disp = Disposable.Empty;

            var bindInfo = Observable.CombineLatest(
                @this,
                view.SubscribeToExpressionChain <TView, object>(controlExpression, false, false, RxApp.SuppressViewCommandBindingMessage).Select(x => x.Value),
                (val, host) => new { val, host });

            var propSub = bindInfo
                          .Where(x => x.host != null)
                          .Subscribe(x =>
            {
                disp.Dispose();
                if (x == null)
                {
                    disp = Disposable.Empty;
                    return;
                }

                var cmd = commandFixuper != null ? commandFixuper(x.val) : x.val;
                if (toEvent != null)
                {
                    disp = CreatesCommandBinding.BindCommandToObject(cmd, x.host, withParameter.Select(y => (object)y), toEvent);
                }
                else
                {
                    disp = CreatesCommandBinding.BindCommandToObject(cmd, x.host, withParameter.Select(y => (object)y));
                }
            });

            return(Disposable.Create(() =>
            {
                propSub.Dispose();
                disp.Dispose();
            }));
        }
Example #3
0
        private static IDisposable BindCommandInternal <TView, TProp, TParam>(
            IObservable <TProp> source,
            TView view,
            Expression controlExpression,
            IObservable <TParam> withParameter,
            string?toEvent)
            where TView : class, IViewFor
            where TProp : ICommand
        {
            var disposable = Disposable.Empty;

            var bindInfo = source.CombineLatest(
                view.SubscribeToExpressionChain <TView, object?>(controlExpression, false, false, RxApp.SuppressViewCommandBindingMessage).Select(x => x.GetValue()),
                (val, host) => new { val, host });

            var propSub = bindInfo
                          .Subscribe(x =>
            {
                if (x.host is null)
                {
                    return;
                }

                disposable.Dispose();
                if (x is null)
                {
                    disposable = Disposable.Empty;
                    return;
                }

                disposable = !string.IsNullOrEmpty(toEvent) ?
                             CreatesCommandBinding.BindCommandToObject(x.val, x.host, withParameter.Select(y => (object)y !), toEvent) :
                             CreatesCommandBinding.BindCommandToObject(x.val, x.host, withParameter.Select(y => (object)y !));
            });

            return(Disposable.Create(() =>
            {
                propSub.Dispose();
                disposable.Dispose();
            }));
        }