Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8677057
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T20:21:23+00:00 2026-06-12T20:21:23+00:00

I want to use MVVM in a WinRT (Windows 8) app, and one of

  • 0

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.Action
1[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 ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T20:21:24+00:00Added an answer on June 12, 2026 at 8:21 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am new in MVVM. I just learn this pattern and want to use
I have one user control in wpf. And I want to use this user
Can I use UnityContainer in View's codebehind when I want to write good MVVM
I want use this 1 for using Bar code or QR code scanner. I
How to use MVVM in WPF Browser Application? I want to know how to
I began to use MVVM pattern yesterday.But for working with events i needed to
I use MVVM pattern in my WPF application. I'm trying to cache my windows
When writing a WPF application using MVVM, I want to use a WCF service,
I don't want to use MVVM and want to change the selected row Foreground
I am writing a Windows Phone application and planned to use some MVVM idea

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.