连接 ReactiveCommand 以调用 Web 服务的最佳方法是什么

What is the best way to wire up ReactiveCommand to invoke a webservice

我需要一些帮助来了解为 WPF 项目连接和使用 reactiveui 的最新推荐方法。

在互联网上研究 reactiveui 时,我遇到了跨越很长一段时间的各种(少数)帖子,在此期间库不断发展,不幸的是,其中一些操作方法文章现在引用了旧的做事方式不再适用的东西

我正在尝试了解连接命令的推荐方法(通常是调用 returns DTO 的 Web 服务)并且我发现了多种方法。

我目前的理解是

// this is the first thing to do
MyCommand =  ReactiveCommand.Create()

// variations to wire up the delegates / tasks to be invoked
MyCommand.CreateAsyncTask()  
MyCommand.CreateAsyncFunc()  
MyCommand.CreateAsyncAction()  

// this seems to be only way to wire handler for receiving result
MyCommand.Subscribe  

// not sure if these below are obsolete?
MyCommand.ExecuteAsync  
MyCommand.RegisterAsyncTask()

有人可以解释一下这些变体中哪些是最新的 API 哪些是过时的,也许可以简单地说明一下何时使用它们

ReactiveCommand 的更改 API 记录在此博客 post 中: http://log.paulbetts.org/whats-new-in-reactiveui-6-reactivecommandt/

第一个选项 - ReactiveCommand.Create() - 只是创建一个反应命令。 要定义一个从服务中异步 returns 数据的命令,您可以使用:

MyCommand = ReactiveCommand.CreateAsyncTask(
                 canExec,  // optional 
                 async _ => await api.LoadSomeData(...));

您可以在收到数据时使用 Subscribe 方法处理数据:

this.Data = new ReactiveList<SomeDTO>();
MyCommand.Subscribe(items => 
{
    this.Data.Clear();
    foreach (var item in items)
        this.Data.Add(item);
}

不过,最简单的方法是使用 ToProperty 方法,如下所示:

this._data = MyCommand
                .Select(items => new ReactiveList<SomeDTO>(items))
                .ToProperty(this, x => x.Data); 

您在其中为数据定义了输出 属性:

private readonly ObservableAsPropertyHelper<ReactiveList<SomeDTO>> _data;
public ReactiveList<SomeDTO> Data
{
    get { return _data.Value; }
}