c# - Register and deregister an event with reflection in WinRT -


For my open source project, I am trying to implement a better page class for WinRT and WinPRT (8.1) . The square is located in a PCL library, this is why I have to register the backed-down event by reflection without the reflection code looks like this:

  using Windows.Phone.UI. Input; Namespace Sample Universal Phone App {Public Square Mycelace {Public Zero Start (HardwareButton. Backed + OnBacked;} Public Zero Stop () {HardwareButton. Backpoked - = On Backpress;} Private Zero OnBackpressed (Object Sender, BackPressedEventArgs E) { // TODO: Add more code e.Handled = true;}}   

}

Now I need exact logic but due to reflection hardwarebutton < / Code> and BackPressedEventArgs classes are available to me ICL is not available in the library. I

How can I do this?

Here's a solution to this question:

  Public category MyClass {Personal object _backPressedToken; Private StaticType only to read _ HardwareButton Type; Private Static Readonly Event InfoBackPressAvent; Static MyClass () { _hardwareButtonsType = Type.GetType ("Windows.Phone.UI.Input.HardwareButtons," + "Windows, version = 255.255.255.255, Culture = Neutral," + "Public Token = zero, contentType = WindowsRuntime "); If (_HardwareButtonsType! = Null) _backPressedEvent = _hardwareButtonsType.GetRuntimeEvent ("Backpress"); } Public Zero Starter () {// Register event action & lt; Object, object & gt; Callback = onbackkeysaid; Var callbackMethodInfo = callback.GetMethodInfo (); Var backPressedDelegate = callbackMethodInfo.CreateDelegate (_backPressedEvent.EventHandlerType, this); _backPressedToken = _backPressedEvent.AddMethod.Invoke (empty, new object [] {backPressedDelegate}); } Public zero-stop () {// unregistered event _backPressedEvent.RemoveMethod.Invoke (tap, new [] {_backPressedToken}); _backPressedToken = Faucet; } Private Zero OnBackKeyPressed (Object Sender, Dynamic Elgies) {// handle event if args.Handled! Args.Handled = DoIt (args.Handled); }}   

Finally used in this class:



Comments

Popular posts from this blog

Pass DB Connection parameters to a Kettle a.k.a PDI table Input step dynamically from Excel -

multithreading - PhantomJS-Node in a for Loop -

c++ - MATLAB .m file to .mex file using Matlab Compiler -