MrL*_*uje 3 c# interaction winforms reactiveui
因此,我正在学习将 ReactiveUI 7.4 与 winforms 一起使用,并且我认为我获得了很好的体验,直到我尝试包含用于显示错误弹出窗口的交互:
视图模型
public class ViewModel : ReactiveObject
{
[...]
public ViewModel()
{
GetCmd = ReactiveCommand.CreateFromTask(
_ => myAsyncFunc(),
CanGetCmd
);
GetCmd.ThrownExceptions
.Subscribe(ex => Interactions.Errors.Handle(ex)); <- breakpoint correctly breaks in there
}
}
Run Code Online (Sandbox Code Playgroud)
互动
public static class Interactions
{
public static readonly Interaction<Exception, Unit> Errors = new Interaction<Exception, Unit>();
}
Run Code Online (Sandbox Code Playgroud)
看法
public ViewCtor()
{
[viewmodel declaration...]
this.btnGet.Events().Click
.Select(_ => Unit.Default)
.InvokeCommand(this, x => x.ViewModel.GetCmd);
Interactions.Errors.RegisterHandler(interaction =>
{
_popupManager.Show(interaction.Input.Message, "Arf !"); <- but breakpoint never hits here :(
});
}
Run Code Online (Sandbox Code Playgroud)
基本上在调试中,断点会在Handle声明中命中,但不会在RegisterHandler函数中命中。
我可能错过了一些东西,因为从关于交互的ReactiveUI 文档中,如果我没有设置任何 RegisterHandler (我尝试过),我应该得到一个 UnhandledInteractionException 并且我什至没有得到这个异常......
如果给定交互没有处理程序,或者没有处理程序设置结果,则交互本身被视为未处理。在这种情况下,调用 Handle 将导致抛出 UnhandledInteractionException。
(我还使用reactiveui-events-winforms来更好地连接事件语法)
小智 8
Interactions.Errors.Handle(ex)返回一个冷的可观察值,即在您订阅它之前它实际上不会执行任何操作。这应该有效:
GetCmd.ThrownExceptions
.Subscribe(ex => Interactions.Errors.Handle(ex).Subscribe());
Run Code Online (Sandbox Code Playgroud)
(您可能需要添加using System;。)