Warm tip: This article is reproduced from stackoverflow.com, please click
uwp-xaml

Where is the FocusManager in a uwp xaml applications?

发布于 2020-03-29 12:46:17

I'm trying to solve a problem in a xaml / uwp application. Most of the suggestions I'm finding refer to a class called FocusManager, which appears to be a property of many controls and containers.

Unfortunately, whenever I try to use it, I find that there is no such property available in my application on any of my controls, including Page, UserControl, StackPanel, etc.

I'm confused as to why this is.

How do I access this class in my application? Or, is there a different way to change the focus?

Questioner
Frank LaRosa
Viewed
51
Nico Zhu - MSFT 2020-01-31 16:30

Please check this FocusManager document. It is a helper class that enables global management of focus actions and events across all elements in an application, but not a property of the control or page. And the following is demo code that could test.

private void Page_KeyUp(object sender, KeyRoutedEventArgs e)
{
   if (e.Key == Windows.System.VirtualKey.Up)
   {
      // Mimic Shift+Tab when user hits up arrow key.
      FocusManager.TryMoveFocus(FocusNavigationDirection.Previous);
   }
   else if (e.Key == Windows.System.VirtualKey.Down)
   {
      // Mimic Tab when user hits down arrow key.
      FocusManager.TryMoveFocus(FocusNavigationDirection.Next);
   }
}