我正在使用ReactiveUI。在一个按钮上,我想调用一个Web服务。如果此调用成功,我想更新我的UI。
当使用RegisterAsyncFunction
注册异步函数时,您将获得一个可以订阅的可观察对象。这使您有机会在异步代码返回时运行代码,您还可以ObserveOnDispatcher()
在UIThread上运行代码。
唯一的问题是 - 我的命令没有返回值。
我认为RegisterAsyncAction
就是为了这个目的,但我找不到任何方法知道这个动作何时完成。
在这种情况下,使用ReactiveAsyncCommnand的正确方法是什么?
答案 0 :(得分:1)
hwhoops,看起来您在RegisterAsyncAction
中发现了一个错误,它应该返回IObservable<Unit>
与此同时,只需将修正后的版本复制粘贴到您的应用中,它只是一个扩展方法:
/// <summary>
/// RegisterAsyncAction registers an asynchronous method that runs
/// whenever the Command's Execute method is called and doesn't return a
/// result.
/// </summary>
/// <param name="calculationFunc">The function to be run in the
/// background.</param>
public static IObservable<Unit> RegisterAsyncAction(this IReactiveAsyncCommand This,
Action<object> calculationFunc,
IScheduler scheduler = null)
{
return This.RegisterAsyncFunction(x => { calculationFunc(x); return Unit.Default; }, scheduler);
}
您还可以
ObserveOnDispatcher()
在UIThread上运行代码。
您实际上不需要这样做,RxUI已经保证Register*
的结果将返回UI线程。