我真的开始挖掘这个rx的东西......基本上,我跟着this video一起学习了一些关于ReactiveUI的知识,然后开始使用它真的!ReactiveUI 7.0如何处理抛出异常时处置的observables
我试图创建一个情况,当我们使用WhenAnyValue来执行一个限制搜索你的类型。而且,如果搜索函数抛出一个异常,我想在视图模型上设置一个名为IsError
的属性(这样我可以显示一个X或某个东西)。这是我工作的ViewModel的重要部分:
public ReactiveCommand<string, IEnumerable<DictItem>> SearchCmmand;
... in vm constructor:
//create our command async from task. executes on worker thread
SearchCmmand = ReactiveCommand.CreateFromTask<string, IEnumerable<DicItem>>(async x => {
this.IsError = false;
//this may throw an exception:
return await GetFilteredAsync(this.SearchText);
});
//SearchCommand is subscribable.
//set the Filtered Items property. executes on main thread
SearchCmmand.Subscribe(filteredItems => {
this.FilteredItems = filteredItems;
});
//any unhandled exceptions that are thown in SearchCommand will bubble up through the ThrownExceptions observable
SearchCmmand.ThrownExceptions.Subscribe(ex=> {
this.IsError = true;
//but after this, then the WhenAnyValue no longer continues to work.
//how to get it back?
});
//invoke the command when SearchText changes
this.WhenAnyValue(v => v.SearchText)
.Throttle(TimeSpan.FromMilliseconds(500))
.InvokeCommand(SearchCmmand);
而且这个工作。当我的GetFilteredAsync
引发异常时,SearchCmmand.ThrownExceptions
被调用,我可以设置我的IsError
属性。
但是,当SearchCmmand.ThrownExceptions
第一次发生时,this.WhenAnyValue(v => v.SearchText)
停止工作。我可以看到它被丢弃。对SearchText的后续更改不会调用该命令。 (虽然命令仍然有效,如果我有一个按钮绑定到它)
看来这是打算的行为,但我们怎么能得到可观察的工作呢?我意识到我可以把它全部包装在try/catch中,并返回一些非例外的东西,但是,我在video(约39:03)看到,在他的情况下,searchtext在抛出异常后继续工作? (该vid的源代码是here)。
我也看到here东西约UserError
,但现在标记为遗产。