FocusManager.TryMoveFocusAsync Method
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Overloads
TryMoveFocusAsync(FocusNavigationDirection) |
Asynchronously attempts to change focus from the current element with focus to the next focusable element in the specified direction. |
TryMoveFocusAsync(FocusNavigationDirection, FindNextElementOptions) |
Asynchronously attempts to change focus from the current element with focus to the next focusable element in the specified direction and subject to the specified navigation options. |
TryMoveFocusAsync(FocusNavigationDirection)
Asynchronously attempts to change focus from the current element with focus to the next focusable element in the specified direction.
public:
static IAsyncOperation<FocusMovementResult ^> ^ TryMoveFocusAsync(FocusNavigationDirection focusNavigationDirection);
/// [Windows.Foundation.Metadata.Overload("TryMoveFocusAsync")]
/// [Windows.Foundation.Metadata.RemoteAsync]
static IAsyncOperation<FocusMovementResult> TryMoveFocusAsync(FocusNavigationDirection const& focusNavigationDirection);
[Windows.Foundation.Metadata.Overload("TryMoveFocusAsync")]
[Windows.Foundation.Metadata.RemoteAsync]
public static IAsyncOperation<FocusMovementResult> TryMoveFocusAsync(FocusNavigationDirection focusNavigationDirection);
function tryMoveFocusAsync(focusNavigationDirection)
Public Shared Function TryMoveFocusAsync (focusNavigationDirection As FocusNavigationDirection) As IAsyncOperation(Of FocusMovementResult)
Parameters
- focusNavigationDirection
- FocusNavigationDirection
The direction that focus moves from element to element within the app UI.
Returns
The FocusMovementResult that indicates whether focus was successfully set.
- Attributes
Examples
Here, we show how to set focus on a WebView object, and, if that fails, restore focus to the original element.
async void MoveFocus(WebView webView))
{
FocusMovementResult result;
result = await FocusManager.TryFocusAsync(webView, FocusState.Programmatic);
if (!result.Succeeded)
{
// Restore focus to original element.
this.Focus(FocusState.Programmatic);
}
}
Remarks
- FocusManager.TryMoveFocus methods return true (success), before the focus change is complete.
- GetFocusedElement does not return the newly focused object until the asynchronous operation completes.
- The control losing focus receives its LosingFocus event synchronously, but does not receive LostFocus until the asynchronous operation completes.
- The control getting focus receives its GettingFocus event synchronously, but does not receive GotFocus until the asynchronous operation completes.
TryFocusAsync completes synchronously when called on an element running in the app process.
You can also use either the FindNextElement(FocusNavigationDirection, FindNextElementOptions) method or the FindNextElement(FocusNavigationDirection) method to programmatically move focus. These methods retrieve the element (as a DependencyObject) that will receive focus based on the specified navigation direction (directional navigation only, cannot be used to emulate tab navigation).
Note
We recommend using the FindNextElement method instead of FindNextFocusableElement
because FindNextFocusableElement
retrieves a UIElement
, which returns null
if the next focusable element is not a UIElement
(such as a Hyperlink object).
See also
- TryMoveFocusAsync(FocusNavigationDirection, FindNextElementOptions)
- Keyboard interactions
- Focus navigation for keyboard, gamepad, remote control, and accessibility tools
- Programmatic focus navigation
Applies to
TryMoveFocusAsync(FocusNavigationDirection, FindNextElementOptions)
Asynchronously attempts to change focus from the current element with focus to the next focusable element in the specified direction and subject to the specified navigation options.
public:
static IAsyncOperation<FocusMovementResult ^> ^ TryMoveFocusAsync(FocusNavigationDirection focusNavigationDirection, FindNextElementOptions ^ focusNavigationOptions);
/// [Windows.Foundation.Metadata.Overload("TryMoveFocusWithOptionsAsync")]
/// [Windows.Foundation.Metadata.RemoteAsync]
static IAsyncOperation<FocusMovementResult> TryMoveFocusAsync(FocusNavigationDirection const& focusNavigationDirection, FindNextElementOptions const& focusNavigationOptions);
[Windows.Foundation.Metadata.Overload("TryMoveFocusWithOptionsAsync")]
[Windows.Foundation.Metadata.RemoteAsync]
public static IAsyncOperation<FocusMovementResult> TryMoveFocusAsync(FocusNavigationDirection focusNavigationDirection, FindNextElementOptions focusNavigationOptions);
function tryMoveFocusAsync(focusNavigationDirection, focusNavigationOptions)
Public Shared Function TryMoveFocusAsync (focusNavigationDirection As FocusNavigationDirection, focusNavigationOptions As FindNextElementOptions) As IAsyncOperation(Of FocusMovementResult)
Parameters
- focusNavigationDirection
- FocusNavigationDirection
The direction that focus moves from element to element within the app UI.
- focusNavigationOptions
- FindNextElementOptions
The navigation options used to identify the focus candidate.
Returns
The FocusMovementResult that indicates whether focus was successfully set.
- Attributes
Examples
Here, we show how to set focus on a WebView object, and, if that fails, restore focus to the original element.
async void MoveFocus(WebView webView))
{
FocusMovementResult result;
result = await FocusManager.TryFocusAsync(webView, FocusState.Programmatic);
if (!result.Succeeded)
{
// Restore focus to original element.
this.Focus(FocusState.Programmatic);
}
}
Remarks
- FocusManager.TryMoveFocus methods return true (success), before the focus change is complete.
- GetFocusedElement does not return the newly focused object until the asynchronous operation completes.
- The control losing focus receives its LosingFocus event synchronously, but does not receive LostFocus until the asynchronous operation completes.
- The control getting focus receives its GettingFocus event synchronously, but does not receive GotFocus until the asynchronous operation completes.
TryFocusAsync completes synchronously when called on an element running in the app process.
You can also use either the FindNextElement(FocusNavigationDirection, FindNextElementOptions) method or the FindNextElement(FocusNavigationDirection) method to programmatically move focus. These methods retrieve the element (as a DependencyObject) that will receive focus based on the specified navigation direction (directional navigation only, cannot be used to emulate tab navigation).
Note
We recommend using the FindNextElement method instead of FindNextFocusableElement
because FindNextFocusableElement
retrieves a UIElement
, which returns null
if the next focusable element is not a UIElement
(such as a Hyperlink object).
See also
- TryMoveFocusAsync(FocusNavigationDirection)
- Keyboard interactions
- Focus navigation for keyboard, gamepad, remote control, and accessibility tools
- Programmatic focus navigation