public static async Task <bool> SimulateClickAsync(this ButtonBase button, JoinableTaskFactory joinableTaskFactory) { await joinableTaskFactory.SwitchToMainThreadAsync(); if (!button.IsEnabled || !button.IsVisible) { return(false); } if (button is RadioButton radioButton) { ISelectionItemProvider peer = new RadioButtonAutomationPeer(radioButton); peer.Select(); } else if (button is Button button2) { IInvokeProvider peer = new ButtonAutomationPeer(button2); peer.Invoke(); } else { button.RaiseEvent(new RoutedEventArgs(ButtonBase.ClickEvent)); ExecuteCommandSource(button, true); } // Wait for changes to propagate await Task.Yield(); return(true); }
private void Shell_KeyDown(object sender, KeyRoutedEventArgs e) { FocusNavigationDirection direction = FocusNavigationDirection.None; switch (e.Key) { case Windows.System.VirtualKey.Space: case Windows.System.VirtualKey.Enter: { var control = FocusManager.GetFocusedElement() as Control; var option = control as RadioButton; if (option != null) { var automation = new RadioButtonAutomationPeer(option); automation.Select(); } } return; // Otherwise. find next focusable element in apprpriate direction case Windows.System.VirtualKey.Left: case Windows.System.VirtualKey.GamepadDPadLeft: case Windows.System.VirtualKey.GamepadLeftThumbstickLeft: case Windows.System.VirtualKey.NavigationLeft: direction = FocusNavigationDirection.Left; break; case Windows.System.VirtualKey.Right: case Windows.System.VirtualKey.GamepadDPadRight: case Windows.System.VirtualKey.GamepadLeftThumbstickRight: case Windows.System.VirtualKey.NavigationRight: direction = FocusNavigationDirection.Right; break; case Windows.System.VirtualKey.Up: case Windows.System.VirtualKey.GamepadDPadUp: case Windows.System.VirtualKey.GamepadLeftThumbstickUp: case Windows.System.VirtualKey.NavigationUp: direction = FocusNavigationDirection.Up; break; case Windows.System.VirtualKey.Down: case Windows.System.VirtualKey.GamepadDPadDown: case Windows.System.VirtualKey.GamepadLeftThumbstickDown: case Windows.System.VirtualKey.NavigationDown: direction = FocusNavigationDirection.Down; break; } if (direction != FocusNavigationDirection.None) { var control = FocusManager.FindNextFocusableElement(direction) as Control; if (control != null) { control.Focus(FocusState.Programmatic); e.Handled = true; } } }