简体   繁体   English

ReactiveUI:如何从ReactiveCommand取消可观察对象?

[英]ReactiveUI: How to cancel an Observable from a ReactiveCommand?

I have the following: 我有以下内容:

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

Where ConvertImages(object o) is a private method of the ViewModel that, in turn, calls and returns the result of ConvertImages(IEnumerable<ImageDto> images) in a service class that resizes and compresses the images and returns an IObservable<ImageDto> . 其中ConvertImages(object o)是ViewModel的私有方法,该方法又在服务类中调用并返回ConvertImages(IEnumerable<ImageDto> images) ,该服务类将调整图像的大小并压缩并返回IObservable<ImageDto>

Paul Betts suggests here to implement cancellation via the implementation of my IObservable eg using Observable.create , so can anyone provide an example of how to implement such Observable? Paul Betts建议在此处通过使用IObservable的实现来实现取消,例如使用Observable.create ,那么有人可以举例说明如何实现这种Observable吗?

Also what is the RxUI™ way to signal this cancellation? 还有RxUI™发出取消信号的方式是什么? Should I subscription.Dispose() in CancelConversion.RegisterAsyncAction() ? 我应该subscription.Dispose()CancelConversion.RegisterAsyncAction() Should I add .TakeUntil(CancelConversion.Execution_Signaled) to var process , how can I determine "CancelConversion.Execution_Signaled"? 我应该将.TakeUntil(CancelConversion.Execution_Signaled)添加到var process ,如何确定“ CancelConversion.Execution_Signaled”?

This is fairly easy: 这很简单:

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

Handling the actual cancellation (ie stopping whatever work that ConvertImages is really doing) depends on your implementation of ConvertImages. 处理实际取消(即停止ConvertImages实际所做的任何工作)取决于您对ConvertImages的实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何对来自 ReactiveCommand (ReactiveUI) 的异常进行单元测试? - How to unit test exceptions from ReactiveCommand (ReactiveUI)? 如何在 ReactiveUI 的 ReactiveCommand 中为 canExecute 分配一个 function? - How to assign a function for canExecute in ReactiveCommand in ReactiveUI? ReactiveUI:如何将“异步”参数传递给 ReactiveCommand.CreateFromTask() - ReactiveUI: How to pass an 'async' parameter to ReactiveCommand.CreateFromTask() 如何从 ReactiveCommand 捕获异常? - How to catch exception from ReactiveCommand? 如何直接调用ReactiveUI 7中的ReactiveCommand.Execute()是否正确? - How do I make a direct call to ReactiveCommand.Execute() in ReactiveUI 7 correct? 在ReactiveUI Windows窗体中将EventArgs传递给ReactiveCommand - Passing EventArgs to ReactiveCommand in ReactiveUI Windows Forms 如何取消可观察序列 - How to cancel an observable sequence 如何使用 ReactiveUI 正确取消 ViewModel 停用任务? - How to properly cancel a Task on ViewModel-deactivation with ReactiveUI? 在 ReactiveUI 中,相同的代码行为不同,具体取决于订阅 observable 的方式 - In ReactiveUI the same code behaves differently depending on how observable is subscribed ReactiveUI.Validation:调用 ReactiveCommand 后更新 this.IsValid() - ReactiveUI.Validation: this.IsValid() updated after ReactiveCommand is invoked
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM