ReactiveUI:如何将“异步”参数传递给ReactiveCommand.CreateFromTask()

时间:2019-06-03 13:19:15

标签: c# command reactiveui

我已经这样定义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);

没有成功和同样的问题。

1 个答案:

答案 0 :(得分:0)

只是撞了撞我的头,看起来像是这样:

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);
}

这只是一个测试员...