从另一个命令执行一个命令 [英] Executing a command from another command

查看:58
本文介绍了从另一个命令执行一个命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个hello world"表单(Xamarin 表单),其中包含一些字段和一个提交按钮.有一个可观察的 (CanSave) 控制何时可以执行 SaveChangesCommand.如果在 CanSave 为 false 时按下保存按钮,我想向用户显示一条消息.

I have a 'hello world' form (Xamarin forms) with some fields and a submit button. There's an observable (CanSave) which controls when the SaveChangesCommand can execute. If the save button is pressed when the CanSave is false, I want to a display a message to the user.

使用下面的代码,如果我

With the code below, if I

  1. 输入错误的数据
  2. 点击保存(显示错误信息)
  3. 然后更正数据.

CanSave 变为真并执行 SaveChangesCommand - 在再次点击按钮之前.就好像先前被阻止的按钮按下一直在排队,直到 canExecute 变为真.

CanSave becomes true and SaveChangesCommand is executed - before the button is hit again. It's as though the previously blocked button press was queued until canExecute became true.

我错过了什么?

谢谢:-)

 public PersonalProfileModel()
    {
        this.SaveChangesCommand = ReactiveCommand.CreateAsyncTask(this.CanSave(),  message => this.doAllThings(message as string));
        this.ButtonClickedCommand = ReactiveCommand.Create(Observable.Return(true));
        this.ButtonClickedCommand.InvokeCommand(this.SaveChangesCommand);
        // ButtonClickedCommand.Subscribe(x => this.SaveChangesCommand.Execute("hello")); // tried this too
    }

    public ReactiveCommand<object> ButtonClickedCommand { get; set; }
    public ReactiveCommand<string> SaveChangesCommand;

    public IObservable<bool> CanSave()
    {
        var fieldsValid = this.WhenAnyValue(
            x => x.Name,
            x => x.Country,
            (f1, f2) =>
                f1 == "a"
                && f2 == "b");
        return fieldsValid;
    }

    public Task<string> doAllThings(string message)
    {
        var result = Task.Run(() =>{return "hello " + message;});
        return result;
    }

推荐答案

结果证明这是对 ReactiveCommands 和 canExecute 行为的误解.参见 ReactiveCommand 不尊重 canExecute

This turned out to be a misunderstanding in the behaviour of ReactiveCommands and canExecute. See ReactiveCommand not respecting canExecute

这篇关于从另一个命令执行一个命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆