private async void FocusNextClick(object sender, RoutedEventArgs args) { var button = (Button)sender; var content = button.Tag.ToString(); var nextElement = FocusManager.FindNextElement( (FocusNavigationDirection)Enum.Parse(typeof(FocusNavigationDirection), content, true), new FindNextElementOptions() { SearchRoot = ContainerPrimary.XamlRoot.Content }); if (nextElement != null) { var focusMoved = await FocusManager.TryFocusAsync(nextElement, FocusState.Keyboard); var controlName = (nextElement as FrameworkElement)?.Name; if (string.IsNullOrEmpty(controlName)) { controlName = (nextElement as ContentControl)?.Content?.ToString() ?? "N/A"; } MoveResultTextBlock.Text = $"Found focus target {nextElement.GetType().Name} with name {controlName}, focus {(focusMoved.Succeeded ? "moved" : "did not move" )}"; } else { MoveResultTextBlock.Text = $"Could not move focus, no target found."; } }
private void OnThemeButtonKeyDown(object sender, KeyRoutedEventArgs e) { if (e.Key == VirtualKey.Right) { var nextElement = FocusManager.FindNextElement(FocusNavigationDirection.Right); if (nextElement == null) { controlsSearchBox.Focus(FocusState.Programmatic); } } }
//**************** #region Methods /// <summary> /// If Other frame is selected, navigate to it /// </summary> private void NavigationViewControl_SelectionChanged(NavigationView sender, NavigationViewSelectionChangedEventArgs args) { sender.UpdateLayout(); FocusManager.FindNextElement(FocusNavigationDirection.Next); //OnPointerCaptureLost(new PointerRoutedEventArgs("this")); this.ReleasePointerCaptures(); if (args.SelectedItemContainer != null) { //NavigationViewControl.SelectedItem = args.SelectedItemContainer.Tag.ToString(); NavigationViewControl_Navigate(args.SelectedItemContainer.Tag.ToString()); } }
protected void OnItemGridViewKeyDown(object sender, KeyRoutedEventArgs e) { if (e.Key == VirtualKey.Up) { var nextElement = FocusManager.FindNextElement(FocusNavigationDirection.Up); if (nextElement?.GetType() == typeof(Microsoft.UI.Xaml.Controls.NavigationViewItem)) { NavigationRootPage.Current.PageHeader.Focus(FocusState.Programmatic); } else { FocusManager.TryMoveFocus(FocusNavigationDirection.Up); } } }
private void OnSvPanelKeyDown(object sender, KeyRoutedEventArgs e) { if (e.Key == VirtualKey.Up) { var nextElement = FocusManager.FindNextElement(FocusNavigationDirection.Up); if (nextElement.GetType() == typeof(NavigationViewItem)) { NavigationRootPage.Current.PageHeader.Focus(FocusState.Programmatic); } else { FocusManager.TryMoveFocus(FocusNavigationDirection.Up); } } }
private void CommandTextBox_OnKeyUp(object sender, KeyRoutedEventArgs e) { switch (e.Key) { case VirtualKey.Down: case VirtualKey.Up: if (!CommandTextBox.IsSuggestionListOpen) { CommandTextBox.IsSuggestionListOpen = true; } break; case VirtualKey.Enter: if (string.IsNullOrWhiteSpace(CommandTextBox.Text) && !CommandTextBox.IsSuggestionListOpen) { CommandTextBox.IsSuggestionListOpen = true; } else { // TODO: Try to find a better way to handle [Enter] on auto-complete field. // Weird way to move the focus to the primary button... if (!(FocusManager.FindLastFocusableElement(this) is Control secondaryButton) || !secondaryButton.Name.Equals("SecondaryButton")) { return; } secondaryButton.Focus(FocusState.Programmatic); var options = new FindNextElementOptions { SearchRoot = this, XYFocusNavigationStrategyOverride = XYFocusNavigationStrategyOverride.Projection }; if (FocusManager.FindNextElement(FocusNavigationDirection.Left, options) is Control primaryButton) { primaryButton.Focus(FocusState.Programmatic); } } break; } }
private void StepOneButton_Click(object sender, RoutedEventArgs e) { preventLosingFocus = true; StepOneButton.IsEnabled = false; ContainerOne.Visibility = Visibility.Collapsed; ContainerTwo.Visibility = Visibility.Visible; var next = FocusManager.FindNextElement(FocusNavigationDirection.Next); if (next.GetType() == typeof(Microsoft.UI.Xaml.Controls.NavigationViewItem)) { #pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed FocusManager.TryFocusAsync(EmailTextBox, FocusState.Programmatic); #pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed } }
private void OnKeyDown(object sender, KeyRoutedEventArgs e) { DependencyObject candidate = null; var options = new FindNextElementOptions() { SearchRoot = TicTacToeGrid, XYFocusNavigationStrategyOverride = XYFocusNavigationStrategyOverride.Projection }; switch (e.Key) { case Windows.System.VirtualKey.Up: candidate = FocusManager.FindNextElement( FocusNavigationDirection.Up, options); break; case Windows.System.VirtualKey.Down: candidate = FocusManager.FindNextElement( FocusNavigationDirection.Down, options); break; case Windows.System.VirtualKey.Left: candidate = FocusManager.FindNextElement( FocusNavigationDirection.Left, options); break; case Windows.System.VirtualKey.Right: candidate = FocusManager.FindNextElement( FocusNavigationDirection.Right, options); break; } // Also consider whether candidate is a Hyperlink, WebView, or TextBlock. if (candidate != null && candidate is Control) { (candidate as Control).Focus(FocusState.Keyboard); } }
private void ScrollViewer_PreviewKeyDown(object sender, Windows.UI.Xaml.Input.KeyRoutedEventArgs e) { var options = new FindNextElementOptions() { SearchRoot = sender as ScrollViewer, XYFocusNavigationStrategyOverride = XYFocusNavigationStrategyOverride.NavigationDirectionDistance }; switch (e.Key) { case VirtualKey.Up: case VirtualKey.GamepadDPadUp: case VirtualKey.GamepadLeftThumbstickUp: { var candidate = FocusManager.FindNextElement(FocusNavigationDirection.Up, options); if (candidate is ListViewItem) { } e.Handled = true; } break; case VirtualKey.Down: case VirtualKey.GamepadDPadDown: case VirtualKey.GamepadLeftThumbstickDown: { var candidate = FocusManager.FindNextElement(FocusNavigationDirection.Down, options); if (candidate is ListViewItem) { } e.Handled = true; } break; } }
private void JustifiedWrapPanel_KeyDown(object sender, KeyRoutedEventArgs e) { DependencyObject candidate = null; var options = new FindNextElementOptions() { SearchRoot = this, XYFocusNavigationStrategyOverride = XYFocusNavigationStrategyOverride.Projection }; switch (e.Key) { case Windows.System.VirtualKey.Up: candidate = FocusManager.FindNextElement(FocusNavigationDirection.Up, options); break; case Windows.System.VirtualKey.Down: candidate = FocusManager.FindNextElement(FocusNavigationDirection.Down, options); break; case Windows.System.VirtualKey.Left: candidate = FocusManager.FindNextElement(FocusNavigationDirection.Left, options); if (candidate == null) { // Wrap the line and try to move to the previous row candidate = FocusManager.FindNextElement(FocusNavigationDirection.Up, options); if (candidate != null) { while (FocusManager.FindNextElement(FocusNavigationDirection.Right, options) != null) { (candidate as Control).Focus(FocusState.Keyboard); candidate = FocusManager.FindNextElement(FocusNavigationDirection.Right, options); } } } break; case Windows.System.VirtualKey.Right: candidate = FocusManager.FindNextElement(FocusNavigationDirection.Right, options); if (candidate == null) { // Wrap the line and try to move to the ext row candidate = FocusManager.FindNextElement(FocusNavigationDirection.Down, options); if (candidate != null) { while (FocusManager.FindNextElement(FocusNavigationDirection.Left, options) != null) { (candidate as Control).Focus(FocusState.Keyboard); candidate = FocusManager.FindNextElement(FocusNavigationDirection.Left, options); } } } else { // Handle the last row last item cas // Prevent it to move back to last row right most item var oldItem = FocusManager.GetFocusedElement(); (candidate as Control).Focus(FocusState.Keyboard); if (oldItem != FocusManager.FindNextElement(FocusNavigationDirection.Left, options)) { candidate = (DependencyObject)oldItem; } } break; } //Note you should also consider whether is a Hyperlink, WebView, or TextBlock. if (candidate != null && candidate is Control) { (candidate as Control).Focus(FocusState.Keyboard); e.Handled = true; } }
private async void ButtonCardItem_PreviewKeyDown(object sender, KeyRoutedEventArgs e) { FlipViewBanner.UpdateLayout(); VerticalRepeater.UpdateLayout(); FindNextElementOptions findNextElementOptions = new FindNextElementOptions { SearchRoot = ScrollViewerHome, XYFocusNavigationStrategyOverride = XYFocusNavigationStrategyOverride.NavigationDirectionDistance }; //Before move focus //Get current item data context var currentItem = (sender as Button).DataContext as ProductItem; var currentFocusElement = FocusManager.GetFocusedElement() as Button; switch (e.OriginalKey) { case VirtualKey.Up: case VirtualKey.GamepadDPadUp: case VirtualKey.GamepadLeftThumbstickUp: case VirtualKey.Down: case VirtualKey.GamepadDPadDown: case VirtualKey.GamepadLeftThumbstickDown: { //if (!canTap) //{ // e.Handled = true; // return; //} //canTap = false; } break; case VirtualKey.Left: case VirtualKey.GamepadDPadLeft: case VirtualKey.GamepadLeftThumbstickLeft: { } break; case VirtualKey.Right: case VirtualKey.GamepadDPadRight: case VirtualKey.GamepadLeftThumbstickRight: { } break; case VirtualKey.GamepadA: case VirtualKey.Enter: case VirtualKey.Space: { } break; default: break; } try { switch (e.OriginalKey) { case VirtualKey.Up: case VirtualKey.GamepadDPadUp: case VirtualKey.GamepadLeftThumbstickUp: { //BringIntoViewOptions options = new BringIntoViewOptions //{ // VerticalOffset = 50.0, // AnimationDesired = true //}; ////var tt = currentFocusElement.TransformToVisual(appWindow.Content); ////Point currentFocusLeftTopPoint = tt.TransformPoint(new Point(appWindow.Bounds.Left, appWindow.Bounds.Top)); ////Rect rect = new Rect(124, currentFocusLeftTopPoint.Y - 500, 1920, 500); ////var nextFocus = FocusManager.FindNextFocusableElement(FocusNavigationDirection.Next, rect); //var candidate = FocusManager.FindNextElement(FocusNavigationDirection.Up, findNextElementOptions); //if (candidate != null && candidate is Button) //{ // (candidate as Control).StartBringIntoView(); // (candidate as Control).Focus(FocusState.Programmatic); // e.Handled = true; //} //else if(candidate != null) //{ // var current = FlipViewBanner.SelectedItem; // var currentContainer = FlipViewBanner.ContainerFromItem(current) as FlipViewItem; // var result = await FocusManager.TryFocusAsync(currentContainer, FocusState.Programmatic); // currentContainer.StartBringIntoView(); // e.Handled = true; //} if (!canTap) { return; } canTap = false; riverIndex--; if (riverIndex < 0) { var current = FlipViewBanner.SelectedItem; var currentContainer = FlipViewBanner.ContainerFromItem(current) as FlipViewItem; var result = await FocusManager.TryFocusAsync(currentContainer, FocusState.Programmatic); currentContainer.StartBringIntoView(); canTap = true; e.Handled = true; return; } try { Debug.WriteLine("River index: " + riverIndex); var grid = VerticalRepeater.TryGetElement(riverIndex) as Grid; if (grid != null) { grid.StartBringIntoView(); var horizontalRepeater = TreeHelper.FindVisualChild <ItemsRepeater>(grid); if (horizontalRepeater != null) { horizontalRepeater.UpdateLayout(); var lastFocusedItem = horizontalRepeater.GetOrCreateElement(listIndexes[riverIndex]); var button = TreeHelper.FindVisualChild <Button>(lastFocusedItem); if (button != null) { button.Focus(FocusState.Programmatic); button.StartBringIntoView(); canTap = true; e.Handled = true; } } } } catch { Debug.WriteLine("Do not scroll too fast!!!"); } } break; case VirtualKey.Down: case VirtualKey.GamepadDPadDown: case VirtualKey.GamepadLeftThumbstickDown: { ////Last river //BringIntoViewOptions options = new BringIntoViewOptions //{ // VerticalOffset = -50.0, // AnimationDesired = true //}; //var candidate = FocusManager.FindNextElement(FocusNavigationDirection.Down, findNextElementOptions); //if (candidate != null && candidate is Control) //{ // (candidate as Control).StartBringIntoView(); // (candidate as Control).Focus(FocusState.Programmatic); // e.Handled = true; //} if (!canTap) { return; } canTap = false; riverIndex++; if (riverIndex > listIndexes.Count - 1) { riverIndex--; e.Handled = true; return; } try { Debug.WriteLine("River index: " + riverIndex); var grid = VerticalRepeater.TryGetElement(riverIndex) as Grid; if (grid != null) { grid.StartBringIntoView(); var horizontalRepeater = TreeHelper.FindVisualChild <ItemsRepeater>(grid); if (horizontalRepeater != null) { horizontalRepeater.UpdateLayout(); var lastFocusedItem = horizontalRepeater.GetOrCreateElement(listIndexes[riverIndex]); var button = TreeHelper.FindVisualChild <Button>(lastFocusedItem); //Scroll to last river if (riverIndex == listIndexes.Count - 1) { ScrollViewerHome.ChangeView(0.0f, ScrollViewerHome.ScrollableHeight, 1.0f); } if (button != null) { button.Focus(FocusState.Programmatic); button.StartBringIntoView(); canTap = true; e.Handled = true; } } } } catch { Debug.WriteLine("Do not scroll too fast!!!"); } } break; case VirtualKey.Left: case VirtualKey.GamepadDPadLeft: case VirtualKey.GamepadLeftThumbstickLeft: { var parent = TreeHelper.FindParentByName <ItemsRepeater>(sender as Button, "HorizontalRepeater"); FindNextElementOptions findNextElementOptions1 = new FindNextElementOptions { SearchRoot = parent, XYFocusNavigationStrategyOverride = XYFocusNavigationStrategyOverride.Projection }; var candidate = FocusManager.FindNextElement(FocusNavigationDirection.Left, findNextElementOptions1); if (candidate != null && candidate is Control) { (candidate as Control).StartBringIntoView(); (candidate as Control).Focus(FocusState.Programmatic); e.Handled = true; } else { ShellPage.shellPage.navigationView.IsPaneOpen = true; e.Handled = true; } } break; case VirtualKey.Right: case VirtualKey.GamepadDPadRight: case VirtualKey.GamepadLeftThumbstickRight: { } break; case VirtualKey.GamepadA: case VirtualKey.Enter: case VirtualKey.Space: { } break; default: break; } } catch { } finally { //Debug.WriteLine(DateTime.Now); //await Task.Delay(1000); //Debug.WriteLine("sss " + DateTime.Now); //canTap = true; } }
private async void ButtonCardItem_PreviewKeyDown(object sender, KeyRoutedEventArgs e) { FlipViewBanner.UpdateLayout(); VerticalRepeater.UpdateLayout(); FindNextElementOptions findNextElementOptions = new FindNextElementOptions { SearchRoot = ScrollViewerHome, XYFocusNavigationStrategyOverride = XYFocusNavigationStrategyOverride.NavigationDirectionDistance }; //Before move focus //Get current item data context var currentItem = (sender as Button).DataContext as ProductItem; var currentFocusElement = FocusManager.GetFocusedElement() as Button; try { switch (e.OriginalKey) { case VirtualKey.Up: case VirtualKey.GamepadDPadUp: case VirtualKey.GamepadLeftThumbstickUp: { BringIntoViewOptions options = new BringIntoViewOptions { VerticalOffset = 50.0, AnimationDesired = true }; //var tt = currentFocusElement.TransformToVisual(appWindow.Content); //Point currentFocusLeftTopPoint = tt.TransformPoint(new Point(appWindow.Bounds.Left, appWindow.Bounds.Top)); //Rect rect = new Rect(124, currentFocusLeftTopPoint.Y - 500, 1920, 500); //var nextFocus = FocusManager.FindNextFocusableElement(FocusNavigationDirection.Next, rect); var candidate = FocusManager.FindNextElement(FocusNavigationDirection.Up, findNextElementOptions); if (candidate != null && candidate is Button) { (candidate as Control).StartBringIntoView(); (candidate as Control).Focus(FocusState.Programmatic); e.Handled = true; } else if (candidate != null) { var current = FlipViewBanner.SelectedItem; var currentContainer = FlipViewBanner.ContainerFromItem(current) as FlipViewItem; var result = await FocusManager.TryFocusAsync(currentContainer, FocusState.Programmatic); currentContainer.StartBringIntoView(); e.Handled = true; } } break; case VirtualKey.Down: case VirtualKey.GamepadDPadDown: case VirtualKey.GamepadLeftThumbstickDown: { //Last river BringIntoViewOptions options = new BringIntoViewOptions { VerticalOffset = -50.0, AnimationDesired = true }; var candidate = FocusManager.FindNextElement(FocusNavigationDirection.Down, findNextElementOptions); if (candidate != null && candidate is Control) { (candidate as Control).StartBringIntoView(); (candidate as Control).Focus(FocusState.Programmatic); e.Handled = true; } } break; case VirtualKey.Left: case VirtualKey.GamepadDPadLeft: case VirtualKey.GamepadLeftThumbstickLeft: { var parent = TreeHelper.FindParentByName <ItemsRepeater>(sender as Button, "HorizontalRepeater"); FindNextElementOptions findNextElementOptions1 = new FindNextElementOptions { SearchRoot = parent, XYFocusNavigationStrategyOverride = XYFocusNavigationStrategyOverride.Projection }; var candidate = FocusManager.FindNextElement(FocusNavigationDirection.Left, findNextElementOptions1); if (candidate != null && candidate is Control) { (candidate as Control).StartBringIntoView(); (candidate as Control).Focus(FocusState.Programmatic); e.Handled = true; } else { ShellPage.shellPage.navigationView.IsPaneOpen = true; e.Handled = true; } } break; case VirtualKey.Right: case VirtualKey.GamepadDPadRight: case VirtualKey.GamepadLeftThumbstickRight: { } break; case VirtualKey.GamepadA: case VirtualKey.Enter: case VirtualKey.Space: { } break; default: break; } } catch { } finally { } }