首页 > 解决方案 > ReactiveUI:如何将“异步”参数传递给 ReactiveCommand.CreateFromTask()

问题描述

我已经定义了ReactiveCommand这样的:

CmdGetTestResult = ReactiveCommand.CreateFromTask<bool>(async detailed =>
{
    var result = await Client.FooAsync(detailed);
});
CmdGetTestResult.ToPropertyEx(this, x => x.GetTestResultResponse);

viewmodel

public bool Detailed { get; set; }

public Unit GetTestResultResponse { [ObservableAsProperty]get; }
public ReactiveCommand<bool, Unit> CmdGetTestResult { get; }

xaml

<Button
    Content="get test result (F4)"
    Command="{Binding Path=ViewModel.CmdGetTestResult}"
    CommandParameter="{Binding Path=ViewModel.Detailed}"/>

该属性detailedtrue(在调试器中检查),但它始终作为false. 我的代码有什么问题?

编辑:

我也试过reactiveui方法:

this.BindCommand(ViewModel, model => model.CmdGetTestResult, window => window.ButtonGetTestResult, model => model.Detailed);

没有任何成功和同样的问题..

标签: c#commandreactiveui

解决方案


刚刚撞到我的头,看起来是这样的:

private readonly ObservableAsPropertyHelper<List<LocationData>> _locations;
public ReactiveCommand<LocationData, List<LocationData>> GetLocations { get; }

public ViewModel 
{
   GetLocations = ReactiveCommand.CreateFromTask<LocationData, List<LocationData>>(GetLocationsTask);
}

private async Task<List<LocationData>> GetLocationsTask(LocationData i)
{
       return await LocationService.GetLocationsAsync(i);
}

这只是一个测试...


推荐阅读