自定义事件
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));
}
}
不用插入代码方式贴代码,代码又长真切读不下去。