winrt反射

第一步引用扩展类。

using System.Reflection.IntrospectionExtensions;

 

第二步反射。

gridView是我定义的GridView控件。ItemClick是它的ItemClick事件。

var typeInfo = (gridView.GetType()).GetTypeInfo();
            var eventInfo = typeInfo.GetDeclaredEvent("ItemClick");

  

注意它只能获取自己定的事件,父类定义的要用循环获取。

EventInfo eventInfo;

do

{
                    var typeInfo = type.GetTypeInfo();

       eventInfo = typeInfo.GetDeclaredEvent(eventName);
                    type = typeInfo.BaseType;
                }
                while (eventInfo == null && type != null);

 

这样就可以获取了。

 //事件替换

var eventRaisedMethod = this.GetType().GetTypeInfo().GetDeclaredMethod("OnEventRaised");
                return eventRaisedMethod.CreateDelegate(eventInfo.EventHandlerType, this);

posted @ 2014-04-23 18:34  wangjinming  阅读(257)  评论(0编辑  收藏  举报