如何使用 IObservable 订阅 TypedEventHandler<T、TArgs> 事件



我正在尝试以下内容,

var _connectedAnimation = ConnectedAnimationService.GetForCurrentView().GetAnimation("forwardAnimation");
Observable.FromEvent<TypedEventHandler<ConnectedAnimation, object>, object>(
handler => _connectedAnimation.Completed += handler,
handler => _connectedAnimation.Completed -= handler)

但是,当引发事件时,我收到运行时异常

System.ArgumentException: Cannot bind to the target method because its signature or security transparency is not compatible with that of the delegate type.
at System.Delegate.CreateDelegate(Type type, Object firstArgument, MethodInfo method, Boolean throwOnBindFailure)
at System.Reactive.ReflectionUtils.CreateDelegate[TDelegate](Object o, MethodInfo method) in D:a1sRx.NETSourcesrcSystem.ReactiveInternalReflectionUtils.cs:line 24
at System.Reactive.Linq.ObservableImpl.FromEvent`2.GetHand

已完成的事件定义为

public sealed class ConnectedAnimation : IConnectedAnimation, IConnectedAnimation2, IConnectedAnimation3
{
/// <summary>Occurs when the animation is finished.</summary>
public extern event TypedEventHandler<ConnectedAnimation, object> IConnectedAnimation.Completed;
}

由于它不是标准事件,并且由于有多个事件参数,因此我们需要创建一个Tuple。请参阅 4.2.3.Rx.Net in Action中具有多个参数的事件。

Observable.FromEvent<TypedEventHandler<ConnectedAnimation, object>, Tuple<ConnectedAnimation, object>>(
rxHandler => (animation, o)=> rxHandler(Tuple.Create(animation,o)),
handler => _connectedAnimation.Completed += handler,
handler => _connectedAnimation.Completed -= handler)

相关内容

  • 没有找到相关文章

最新更新