【问题标题】:How to implement command pattern in the newest versions of ReactiveUI?如何在最新版本的 ReactiveUI 中实现命令模式?
【发布时间】:2021-04-24 16:38:16
【问题描述】:

我正在关注官方 GitHub 页面上给出的关于使用下面的 ReactiveUI.ReactiveCommand 的示例。 (ReactiveUI v8.0.1)github link

public class MainViewModel : ReactiveObject
{
    public ReactiveCommand ParameterlessCommand { get; }
    public MainViewModel()
    {
        ParameterlessCommand = ReactiveCommand.Create(Parameterless);
    }


    private void Parameterless()
    {
        
    }
}

当我将此实现与 ReactiveUI v13.0.38 一起使用时会发生错误

  1. 第一个错误是“CS0722 'ReactiveCommand':静态类型不能 用作返回类型”。
  2. 第二个错误是“CS0029 不能隐式转换类型 'ReactiveUI.ReactiveCommand' 到 'ReactiveUI.ReactiveCommand'"

如何将这个 MVVM 代码示例中的命令模式与 ReactiveUI v13.0.38 一起使用?

【问题讨论】:

  • 我们删除了非通用的 ReactiveCommand。你使用 Unit 作为一个通用类型,就像答案一样。

标签: c# .net winforms reactiveui command-pattern


【解决方案1】:

实现ICommand接口的ReactiveCommand类是泛型类型。

// Unit is used for void - no input and output, so double Unit
public ReactiveCommand<Unit, Unit> ParameterlessCommand { get; }

// assigning
ParameterlessCommand = ReactiveCommand.Create(Parameterless);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-01-13
    • 1970-01-01
    • 1970-01-01
    • 2020-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-07
    相关资源
    最近更新 更多