首页 新闻 会员 周边 捐助

如何让自定义的事件支持MVVM绑定ICommand

0
悬赏园豆:10 [待解决问题]

自定义事件

public event RoutedEventHandler GetSMSCode;

想让他支持MVVM绑定ICommand

MVVM的小架子是是用附加属性实现的,当执行到

 EventInfo ei = LookForEventDeclaration(target, eventName);                 if (ei != null)                 {                     var handler = GetEventMethod(ei);

                    WindowsRuntimeMarshal.AddEventHandler(                         d => (EventRegistrationToken)ei.AddMethod.Invoke(target, new object[] { d }),                         et => ei.RemoveMethod.Invoke(target, new object[] { et }), handler);                     //ei.AddEventHandler(target, handler);                     return;                 }

报了个错,        (EventRegistrationToken)ei.AddMethod    无法将类型“System.Reflection.MethodInfo”转换为“System.Runtime.InteropServices.WindowsRuntime.EventRegistrationToken”     如何解决啊

 

另外:

private EventRegistrationTokenTable<EventHandler<NumberChangedEventArgs>>
        m_NumberChangedTokenTable = null;

    public event EventHandler<NumberChangedEventArgs> NumberChanged
    {
        add
        {
            return EventRegistrationTokenTable<EventHandler<NumberChangedEventArgs>>
                .GetOrCreateEventRegistrationTokenTable(ref m_NumberChangedTokenTable)
                .AddEventHandler(value);
        }
        remove
        {
            EventRegistrationTokenTable<EventHandler<NumberChangedEventArgs>>
                .GetOrCreateEventRegistrationTokenTable(ref m_NumberChangedTokenTable)
                .RemoveEventHandler(value);
        }
    }

提示 add方法是void类型了,是用return编译都过不了的。

    internal void OnNumberChanged(int newValue)
    {
        EventHandler<NumberChangedEventArgs> temp =
            EventRegistrationTokenTable<EventHandler<NumberChangedEventArgs>>
            .GetOrCreateEventRegistrationTokenTable(ref m_NumberChangedTokenTable)
            .InvocationList;
        if (temp != null)
        {
            temp(this, new NumberChangedEventArgs(newValue));
        }
    }

狼行拂晓--JCH的主页 狼行拂晓--JCH | 初学一级 | 园豆:192
提问于:2013-09-24 16:50
< >
分享
所有回答(1)
0

不用插入代码方式贴代码,代码又长真切读不下去。

徐牛 | 园豆:282 (菜鸟二级) | 2013-09-25 11:35
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册