简体   繁体   English

在序列停止响应式 UI 中生成的元素后的一段时间后,如何获取最后一个 Observable 序列值?

[英]How to take last Observable sequence value after some time after sequence stopped produced elements in Reactive UI?

I need to disable WPF UI in my application when any of the 2 ReactiveCommands on the page is executed.当页面上的 2 个 ReactiveCommands 中的任何一个被执行时,我需要在我的应用程序中禁用 WPF UI。 Commands are called one by one.命令被一一调用。 It means that they are invoked the following way:这意味着它们的调用方式如下:

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

I made an observable like:我做了一个可观察的,如:

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

and bound it to the Window.IsEnabled property.并将其绑定到 Window.IsEnabled 属性。

Generally, it works, but the problem that the observable returns value true when 1 command finished its job, but second did not start yet.一般来说,它是有效的,但是当 1 个命令完成其工作时,observable 返回值true的问题,但第二个还没有开始。 So, the overall output is:所以,总的输出是:

true;真的; // Before start // 开始前

false;错误的; // First command executing // 执行第一个命令

true;真的; // First command finished, second not started yet (It is problem) //第一个命令完成,第二个还没有开始(这是问题)

false;错误的; // Second command executing // 执行第二条命令

true;真的; // Both commands finished // 两个命令都完成了

I need to listen to sequence and only after half-second or so from the last real event I should publish an update in mine CanInteractWithUserObservable to avoid UI blinking.我需要听序列,并且只有在距离最后一个真实事件半秒左右后,我才应该在我的 CanInteractWithUserObservable 中发布更新以避免 UI 闪烁。

PS: Probably Timeout method may help me, but I didn't get how to use it. PS:可能超时方法可能对我有帮助,但我不知道如何使用它。

If i understand correctly you want to wait both 2 event to be complete and then let your UI released.如果我理解正确,您希望等待两个事件完成,然后让您的 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 is used for to emitting error when an observable doesn't emit a value for amount of given value. Timeout用于在 observable 未发出给定值数量的值时发出错误。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM