/// <summary>Call this method in Loaded event as the event will be automatically /// deregistered when the FrameworkElement has been unloaded. </summary> public static void RegisterBackKey(Page page) { var callback = new TypedEventHandler <CoreDispatcher, AcceleratorKeyEventArgs>( delegate(CoreDispatcher sender, AcceleratorKeyEventArgs args) { if (!args.Handled && args.VirtualKey == VirtualKey.Back && (args.EventType == CoreAcceleratorKeyEventType.KeyDown || args.EventType == CoreAcceleratorKeyEventType.SystemKeyDown)) { var element = FocusManager.GetFocusedElement(); if (element is FrameworkElement && PopupHelper.IsInPopup((FrameworkElement)element)) { return; } if (element is TextBox || element is PasswordBox || element is WebView) { return; } if (page.Frame.CanGoBack) { args.Handled = true; page.Frame.GoBack(); } } }); page.Dispatcher.AcceleratorKeyActivated += callback; SingleEvent.RegisterEvent(page, (p, h) => p.Unloaded += h, (p, h) => p.Unloaded -= h, (o, a) => { page.Dispatcher.AcceleratorKeyActivated -= callback; }); }
/// <summary>Call this method in Loaded event as the event will be automatically /// deregistered when the FrameworkElement has been unloaded. </summary> /// <param name="page">The page. </param> /// <param name="handler">The event handler. </param> public static void RegisterAcceleratorKeyActivated(FrameworkElement page, TypedEventHandler <CoreDispatcher, AcceleratorKeyEventArgs> handler) { page.Dispatcher.AcceleratorKeyActivated += handler; SingleEvent.RegisterEvent(page, (p, h) => p.Unloaded += h, (p, h) => p.Unloaded -= h, (o, a) => { page.Dispatcher.AcceleratorKeyActivated -= handler; }); }