Dela via


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.

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

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.

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

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

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.

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