3

我正在根据本指南为我的 Windows 8 应用程序实施修改后的 Behavior 版本。除了需要 Reactive 框架的地方之外,它可以工作:

protected override void OnAttached()
{
  var evt = AssociatedObject.GetType().GetRuntimeEvent(Event);
  if (evt != null)
  {
    Observable.FromEventPattern<RoutedEventArgs>(AssociatedObject, Event)
      .Subscribe(se => FireCommand());
  }
  base.OnAttached();
}

问题很简单,如何在没有响应式框架的情况下实现类似的功能?我浏览了可以在此处获得的 Rx 的来源,但对我来说太复杂了。
我还成功移植到代码,唯一的问题是它仅适用于固定类型的 EventHandler:

protected override void OnAttached()
{
    EventInfo evt = AssociatedObject.GetType().GetRuntimeEvent(Event);
    if (evt != null)
    {
        AssignEvent<ItemClickEventHandler>(AssociatedObject, Event, FireCommand);
    }
    base.OnAttached();
}

protected void AssignEvent<T1>(object instance, string eventName, T1 handler)
{
    EventInfo runtimeEvent = instance.GetType().GetRuntimeEvent(eventName);
    Func<T1, EventRegistrationToken> add = a => (EventRegistrationToken)runtimeEvent.AddMethod.Invoke(instance, new object[] { a });
    Action<EventRegistrationToken> remove = a => runtimeEvent.RemoveMethod.Invoke(runtimeEvent, new object[] { a });

    WindowsRuntimeMarshal.AddEventHandler(add, remove, handler);
}

任何想法,如何使它动态,所以我不必使用特定的事件处理程序“ItemClickEventHandler”?请注意,在经典 .NET 中它非常简单,但在 WinRT 中我不能使用Delegate.CreateDelegate(...)

更新:感谢布兰登,我能够完成该方法,现在看起来像这样:

protected override void OnAttached()
{
    EventInfo evt = AssociatedObject.GetType().GetRuntimeEvent(Event);
    if (evt != null)
    {
        MethodInfo addMethod = evt.AddMethod;
        MethodInfo removeMethod = evt.RemoveMethod;
        ParameterInfo[] addParameters = addMethod.GetParameters();
        Type delegateType = addParameters[0].ParameterType;
        Action<object, object> handler = (s, e) => FireCommand(e as RoutedEventArgs);
        MethodInfo handlerInvoke = typeof(Action<object, object>).GetRuntimeMethod("Invoke", new[] { typeof(object), typeof(object) });
        Delegate @delegate = handlerInvoke.CreateDelegate(delegateType, handler);

        Func<object, EventRegistrationToken> add = a => (EventRegistrationToken)addMethod.Invoke(AssociatedObject, new object[] { @delegate });
        Action<EventRegistrationToken> remove = t => removeMethod.Invoke(AssociatedObject, new object[] { t });

        WindowsRuntimeMarshal.AddEventHandler(add, remove, handler);
    }
    base.OnAttached();
}

现在我可以删除 800kB 的 Rx dll,再次感谢!

4

1 回答 1

2

我浏览了 Rx 源代码,这是重要的功能:

MethodInfo addMethod = eventInfo.GetAddMethod();
MethodInfo removeMethod = eventInfo.GetRemoveMethod();
var addParameters = addMethod.GetParameters();
var delegateType = addParameters[0].ParameterType;
Action<object, object> handler = (object sender, object eventArgs) => FireCommand();
MethodInfo handlerInvoke = typeof(Action<object, object>).GetMethod("Invoke");
Delegate delegate = handlerInvoke.CreateDelegate(delegateType, handler);

Func<EventRegistrationToken> add = a => (EventRegistrationToken)addMethod.Invoke(instance, new object[] { delegate });
Action<EventRegistrationToken> remove = t => removeMethod.Invoke(instance, new object[] { t });

看起来重要的信息是他们正在使用MethodInfo.CreateDelegate

于 2013-05-22T15:37:27.237 回答