如何在反应式UI中序列停止生成的元素一段时间后获取最后一个可观察序列值?



当执行页面上的 2 个反应命令中的任何一个时,我需要在我的应用程序中禁用 WPF UI。命令是逐个调用的。这意味着按以下方式调用它们:

Command1
.Select(x => ...Prepare params for second...)
.InvokeCommand(Command2)
.DisposeWith(d);

我做了一个可观察的像:

IObservable<bool> CanInteractWithUserObservable => Command1.IsExecuting
.CombineLatest(Command2.IsExecuting, (c1,c2)=>(c1,c2))
.Select(tuple => tuple.c1 || tuple.c2)
.Select(res => !res)
.ObserveOn(RxApp.MainThread);

并将其绑定到 Window.IsEnabled 属性。

通常,它可以工作,但是当一个命令完成其工作但第二个命令尚未启动时,可观察量返回值true的问题。因此,总体输出为:

true;//在开始之前

false;//第一个命令正在执行

true;//第一个命令已完成,第二个命令尚未启动(这是问题(

false;//第二个命令正在执行

true;//两个命令都已完成

我需要收听序列,并且仅在上次真实事件半秒钟左右后,我应该在我的CanInteractWithUserObservable中发布更新,以避免UI闪烁。

PS:可能超时方法可能对我有所帮助,但我不知道如何使用它。

如果我理解正确,您希望等待两个 2 事件完成,然后让您的 UI 发布。

Command1.IsExecuting
.CombineLatest(Command2.IsExecuting, (c1,c2)=>(c1,c2))
.Select(tuple => tuple.c1 && tuple.c2) // instead of or use and so only when both will be done value will be true
.SkipWhile(t=> t == false) // omit when both not completed 
.Delay(TimeSpan.FromSeconds(.5)) // i don't know you still want to wait 0.5 seconds more but Delay is the operator for that
.Select(res => !res)
.ObserveOn(RxApp.MainThread);

Timeout用于在可观察量未发出给定值量的值时发出错误。

最新更新