【发布时间】:2016-11-26 23:10:29
【问题描述】:
我真的开始研究这个 rx 的东西了...基本上,我跟随 this video 只是为了在我开始真正使用它之前教自己更多关于 ReactiveUI 的知识!
当我们使用 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 的后续更改不会调用该命令。 (尽管如果我绑定了一个按钮,该命令仍然有效)
这似乎是预期的行为,但我们怎样才能让 observable 再次工作呢?我意识到我可以将它全部包装在 try/catch 中并返回不是异常的东西,但是,我在video(大约 39:03)中看到,在他的情况下,searchtext 在异常之后继续工作被抛出? (该视频的源代码是here)。
我还看到 here 一些关于 UserError 的内容,但现在标记为旧版。
【问题讨论】:
标签: system.reactive reactiveui