使用Reactive Extensions,使用Observable.FromEventHandler订阅Control事件非常简单;但是,当涉及到自定义控件的Actions时,我无法使用相同的方法订阅它们的调用,因为它会导致错误说
An exception of type 'System.InvalidOperationException' occurred in System.Reactive.Linq.dll but was not handled in user code
我的自定义控制代码
public partial class Hub : UserControl
{
public Action<bool> OnButtonStartClick { get; set; }
public Hub()
{
Observable.FromEventPattern(buttonHide, "Click", new ControlScheduler(this)).Subscribe(_ =>
{
if (null != OnButtonHideClick)
OnButtonHideClick.Invoke(true);
});
}
}
尝试订阅如下
private void InitializeEventHandlers()
{
Observable.FromEventPattern(MainHub, "OnButtonStartClick").Subscribe(_ =>
{
//DO SOMETHING AS USUAL!
});
}