Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I attach to a WinRT event dynamically?

I want to use MVVM in a WinRT (Windows 8) app, and one of my requirements is to be able to hook events up to commands (ICommand). This means I have to dynamically add a handler to a WinRT event. There is a nice explanation of how to do that here, but my problem is that the handler type is not known at compile time (ie. it is not always RoutedEventHandler as in that example).

I started to write a generic implementation of that code, where I build the delegates using expression trees. That part works. My problem is that invoking WindowsRuntimeMarshal.AddEventHandler dynamically fails:

var rtMarshalType = typeof (WindowsRuntimeMarshal);
var eventHandlerMethod = rtMarshalType.GetRuntimeMethods().Single(x => x.IsStatic && x.Name == "AddEventHandler");
MethodInfo closedAddMethod = eventHandlerMethod.MakeGenericMethod(handlerType);
closedAddMethod.Invoke(null, new object[] {add, remove, handler});

This fails on the Invoke call and throws an InvalidOperationException with the message:

The API 'System.Runtime.InteropServices.WindowsRuntime.WindowsRuntimeMarshal.AddEventHandler[ItemClickEventHandler](System.Func2[Windows.UI.Xaml.Controls.ItemClickEventHandler,System.Runtime.InteropServices.WindowsRuntime.EventRegistrationToken], System.Action1[System.Runtime.InteropServices.WindowsRuntime.EventRegistrationToken], Windows.UI.Xaml.Controls.ItemClickEventHandler)' cannot be used on the current platform. See http://go.microsoft.com/fwlink/?LinkId=248273 for more information.

I know I have the right types, because when I replace the above 4 lines of code with this (hard-coding the event handler type, which is not what I want), then the code works and the event gets attached as expected:

WindowsRuntimeMarshal.AddEventHandler<ItemClickEventHandler>(add, remove, handler);

For reference, the three parameters is defined as follows (to be sure, it is not a problem with my expression tree building code, I am currently using the explicit definition of these delegates even when trying to dynamic invoke AddEventHandler, still failing):

Func<ItemClickEventHandler, EventRegistrationToken> add =
    a => (EventRegistrationToken) eventInfo.AddMethod.Invoke(instance, new object[] {a});
Action<EventRegistrationToken> remove = a => eventInfo.RemoveMethod.Invoke(instance, new object[] {a});
ItemClickEventHandler handler = (s, args) => command.Execute(args);

Why does the call fail when invoked via reflection and not when I call it directly ?

Is there an alternate solution for dynamically attaching a WinRT event, when the type of event handler is not known at compile time ?

like image 440
driis Avatar asked Sep 30 '12 10:09

driis


1 Answers

Why does the call fail when invoked via reflection and not when I call it directly ?

Answer: WindowsRuntimeMarshal.AddEventHandler is marked with [SecurityCritical] attribute

From Security Considerations for Reflection: http://msdn.microsoft.com/en-us/library/stfy7tfc.aspx

Subject to necessary permissions, code can use reflection to perform the following kinds of access: Access public members that are not security-critical.

So, you cannot use reflection security-critical methods like AddEventHandler even if they are public. You can freely call such methods without using reflection and you can freely reflect your own methods. This is why your solution works.

like image 157
Ark-kun Avatar answered Nov 06 '22 23:11

Ark-kun