windows 8 - How to dynamically bind event to command in WinRT without Reactive framework? -


i'm implementing modified version of behavior windows 8 app according guide. works except 1 place reactive framework required:

protected override void onattached() {   var evt = associatedobject.gettype().getruntimeevent(event);   if (evt != null)   {     observable.fromeventpattern<routedeventargs>(associatedobject, event)       .subscribe(se => firecommand());   }   base.onattached(); } 

the question simple, how achieve similar funcitonality without reactive frmaework? i've browsed source of of rx can obtained here, i'ts complicated me.
i've succeeded porting code problem work fixed type of 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 = => (eventregistrationtoken)runtimeevent.addmethod.invoke(instance, new object[] { });     action<eventregistrationtoken> remove = => runtimeevent.removemethod.invoke(runtimeevent, new object[] { });      windowsruntimemarshal.addeventhandler(add, remove, handler); } 

any idea, how make dynamic don't have use specific event handler "itemclickeventhandler"? note in classic .net it' quite simple, in winrt cannot use delegate.createdelegate(...)

update: brandon able finish method, looks this:

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 routedeventargs);         methodinfo handlerinvoke = typeof(action<object, object>).getruntimemethod("invoke", new[] { typeof(object), typeof(object) });         delegate @delegate = handlerinvoke.createdelegate(delegatetype, handler);          func<object, eventregistrationtoken> add = => (eventregistrationtoken)addmethod.invoke(associatedobject, new object[] { @delegate });         action<eventregistrationtoken> remove = t => removemethod.invoke(associatedobject, new object[] { t });          windowsruntimemarshal.addeventhandler(add, remove, handler);     }     base.onattached(); } 

now can remove 800kb of rx dlls, again!

i trolled through rx source, , here important bit of functionality:

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 = => (eventregistrationtoken)addmethod.invoke(instance, new object[] { delegate }); action<eventregistrationtoken> remove = t => removemethod.invoke(instance, new object[] { t }); 

it looks important info using methodinfo.createdelegate.


Comments