ReactiveUI:如何从ReactiveCommand中取消Observable?

时间:2014-05-23 15:54:02

标签: c# reactiveui

我有以下内容:

Compress = this.WhenAnyValue(x => x.ImagesCount, x => x > 0).ToCommand();
var process = Compress.RegisterAsync(ConvertImages);
var subscription = process.Subscribe(x => Images.Remove(x));

CancelConversion = this.WhenAnyValue(x => x.ImagesCount, x => x.IsBusy, (count, busy) => count > 0 && busy).ToCommand();
CancelConversion.RegisterAsyncAction();

其中ConvertImages(object o)是ViewModel的私有方法,反过来调用并在服务类中返回ConvertImages(IEnumerable<ImageDto> images)的结果,该服务类调整大小并压缩图像并返回IObservable<ImageDto>

Paul Betts建议here通过实施IObservable来实施取消,例如使用Observable.create,任何人都可以提供一个如何实现这样的Observable的例子吗?

RxUI™发出此取消信号的方式是什么? subscription.Dispose()我应该CancelConversion.RegisterAsyncAction()吗?我应该将.TakeUntil(CancelConversion.Execution_Signaled)添加到var process,如何确定“CancelConversion.Execution_Signaled”?

1 个答案:

答案 0 :(得分:4)

这很简单:

CancelConversion = this.WhenAnyValue(x => x.ImagesCount, x => x.IsBusy, (count, busy) => count > 0 && busy).ToCommand();

this.WhenAnyValue(x => x.ImagesCount, x => x > 0).ToCommand();

var process = Compress.RegisterAsync(x => ConvertImages().TakeUntil(CancelConversion));
var subscription = process.Subscribe(x => Images.Remove(x));

处理实际取消(即停止ConvertImages正在做的任何工作)取决于您对ConvertImages的实现。