FocusManager.TryMoveFocusAsync Method

Definition

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.

C#
[Windows.Foundation.Metadata.Overload("TryMoveFocusAsync")]
[Windows.Foundation.Metadata.RemoteAsync]
public static IAsyncOperation<FocusMovementResult> TryMoveFocusAsync(FocusNavigationDirection focusNavigationDirection);

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

Windows requirements

Device family
Windows 10, version 1803 (introduced in 10.0.17134.0)
API contract
Windows.Foundation.UniversalApiContract (introduced in v6.0)

Examples

Here, we show how to set focus on a WebView object, and, if that fails, restore focus to the original element.

C#
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

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

Applies to

WinRT Build 26100 and other versions
Product Versions
WinRT Build 17134, Build 17763, Build 18362, Build 19041, Build 20348, Build 22000, Build 22621, Build 26100

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.

C#
[Windows.Foundation.Metadata.Overload("TryMoveFocusWithOptionsAsync")]
[Windows.Foundation.Metadata.RemoteAsync]
public static IAsyncOperation<FocusMovementResult> TryMoveFocusAsync(FocusNavigationDirection focusNavigationDirection, FindNextElementOptions focusNavigationOptions);

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

Windows requirements

Device family
Windows 10, version 1803 (introduced in 10.0.17134.0)
API contract
Windows.Foundation.UniversalApiContract (introduced in v6.0)

Examples

Here, we show how to set focus on a WebView object, and, if that fails, restore focus to the original element.

C#
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

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

Applies to

WinRT Build 26100 and other versions
Product Versions
WinRT Build 17134, Build 17763, Build 18362, Build 19041, Build 20348, Build 22000, Build 22621, Build 26100