public static void AddHandlers(Window window, TouchKeyboardVisibilityHandler showingHandler, TouchKeyboardVisibilityHandler hidingHandler) { VerifySupported(); if (window == null) { throw new ArgumentNullException(nameof(window)); } if (showingHandler == null) { throw new ArgumentNullException(nameof(showingHandler)); } if (hidingHandler == null) { throw new ArgumentNullException(nameof(hidingHandler)); } var winRtShowingHandler = AddShowingHandler(window, showingHandler); var winRtHidingHandler = AddHidingHandler(window, hidingHandler); // Register to unregister the handlers if needed window.Closing += Window_Closing; if (!_registrationTokens.ContainsKey(window)) { _registrationTokens[window] = new List <WinRTEventHandlers>(); } // Keep track of the WinRT handlers _registrationTokens[window].Add(new WinRTEventHandlers() { Showing = winRtShowingHandler, Hiding = winRtHidingHandler }); }
/// <summary> /// Adds a handler for the specific visibility event /// </summary> /// <param name="visibilityEvent">The event to add a handler for</param> /// <param name="window">The window to add the handler for</param> /// <param name="handler">The handler</param> /// <returns>The WinRT handler</returns> private static WinRTVisibilityHandler AddVisibilityHandler(EventInfo visibilityEvent, Window window, TouchKeyboardVisibilityHandler handler) { IInputPane2 inputPane = GetInputPane(window); // This adds the event handler to the visibility event Func<WinRTVisibilityHandler, EventRegistrationToken> add = (addHandler) => { return (EventRegistrationToken)visibilityEvent.AddMethod.Invoke(inputPane, new object[] { addHandler }); }; // This removes the handler from the visibility event Action<EventRegistrationToken> remove = (removeHandler) => { visibilityEvent.RemoveMethod.Invoke(inputPane, new object[] { removeHandler }); }; // This is the WinRT called handler to translate to the local WPF delegate WinRTVisibilityHandler localHandler = (inputPaneInstance, visArgs) => { // We cannot reference System.Runtime.WindowsRuntime directly, so only access the Rect type via // reflection to ensure the proper assembly loads with no issues. var rect = visArgs.GetType().GetProperty("OccludedRect").GetValue(visArgs); // Invoke the WPF specific handler to pass the occluded rectangle information handler?.Invoke( window, new System.Windows.Rect( (double)rect.GetType().GetRuntimeProperty("X").GetValue(rect), (double)rect.GetType().GetRuntimeProperty("Y").GetValue(rect), (double)rect.GetType().GetRuntimeProperty("Width").GetValue(rect), (double)rect.GetType().GetRuntimeProperty("Height").GetValue(rect))); }; // Add the event handlers WindowsRuntimeMarshal.AddEventHandler<WinRTVisibilityHandler>(add, remove, localHandler); // Return the WinRT handler in order to use it to un-register later return localHandler; }
/// <summary> /// Adds a handler for the hiding event /// </summary> /// <param name="window">The window to add the handler for</param> /// <param name="handler">The handler</param> /// <returns>The WinRT handler</returns> private static WinRTVisibilityHandler AddHidingHandler(Window window, TouchKeyboardVisibilityHandler handler) { return AddVisibilityHandler(_hidingEvent, window, handler); }
public static void AddHandlers(Window window, TouchKeyboardVisibilityHandler showingHandler, TouchKeyboardVisibilityHandler hidingHandler) { VerifySupported(); if (window == null) { throw new ArgumentNullException(nameof(window)); } if (showingHandler == null) { throw new ArgumentNullException(nameof(showingHandler)); } if (hidingHandler == null) { throw new ArgumentNullException(nameof(hidingHandler)); } var winRtShowingHandler = AddShowingHandler(window, showingHandler); var winRtHidingHandler = AddHidingHandler(window, hidingHandler); // Register to unregister the handlers if needed window.Closing += Window_Closing; if (!_registrationTokens.ContainsKey(window)) { _registrationTokens[window] = new List<WinRTEventHandlers>(); } // Keep track of the WinRT handlers _registrationTokens[window].Add(new WinRTEventHandlers() { Showing = winRtShowingHandler, Hiding = winRtHidingHandler }); }
/// <summary> /// Adds a handler for the specific visibility event /// </summary> /// <param name="visibilityEvent">The event to add a handler for</param> /// <param name="window">The window to add the handler for</param> /// <param name="handler">The handler</param> /// <returns>The WinRT handler</returns> private static WinRTVisibilityHandler AddVisibilityHandler(EventInfo visibilityEvent, Window window, TouchKeyboardVisibilityHandler handler) { IInputPane2 inputPane = GetInputPane(window); // This adds the event handler to the visibility event Func <WinRTVisibilityHandler, EventRegistrationToken> add = (addHandler) => { return((EventRegistrationToken)visibilityEvent.AddMethod.Invoke(inputPane, new object[] { addHandler })); }; // This removes the handler from the visibility event Action <EventRegistrationToken> remove = (removeHandler) => { visibilityEvent.RemoveMethod.Invoke(inputPane, new object[] { removeHandler }); }; // This is the WinRT called handler to translate to the local WPF delegate WinRTVisibilityHandler localHandler = (inputPaneInstance, visArgs) => { // We cannot reference System.Runtime.WindowsRuntime directly, so only access the Rect type via // reflection to ensure the proper assembly loads with no issues. var rect = visArgs.GetType().GetProperty("OccludedRect").GetValue(visArgs); // Invoke the WPF specific handler to pass the occluded rectangle information handler?.Invoke( window, new System.Windows.Rect( (double)rect.GetType().GetRuntimeProperty("X").GetValue(rect), (double)rect.GetType().GetRuntimeProperty("Y").GetValue(rect), (double)rect.GetType().GetRuntimeProperty("Width").GetValue(rect), (double)rect.GetType().GetRuntimeProperty("Height").GetValue(rect))); }; // Add the event handlers WindowsRuntimeMarshal.AddEventHandler <WinRTVisibilityHandler>(add, remove, localHandler); // Return the WinRT handler in order to use it to un-register later return(localHandler); }
/// <summary> /// Adds a handler for the hiding event /// </summary> /// <param name="window">The window to add the handler for</param> /// <param name="handler">The handler</param> /// <returns>The WinRT handler</returns> private static WinRTVisibilityHandler AddHidingHandler(Window window, TouchKeyboardVisibilityHandler handler) { return(AddVisibilityHandler(_hidingEvent, window, handler)); }