Skip to content

Latest commit

 

History

History
61 lines (40 loc) · 3.61 KB

focusmanager_trymovefocusasync_1020299191.md

File metadata and controls

61 lines (40 loc) · 3.61 KB
-api-id -api-type
M:Windows.UI.Xaml.Input.FocusManager.TryMoveFocusAsync(Windows.UI.Xaml.Input.FocusNavigationDirection)
winrt method

Windows.UI.Xaml.Input.FocusManager.TryMoveFocusAsync

-description

Asynchronously attempts to change focus from the current element with focus to the next focusable element in the specified direction.

-parameters

-param focusNavigationDirection

The direction that focus moves from element to element within the app UI.

-returns

The FocusMovementResult that indicates whether focus was successfully set.

-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).

-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); 
    } 
}

-see-also

FocusManager.TryMoveFocusAsync(FocusNavigationDirection focusNavigationDirection, FindNextElementOptions focusNavigationOptions), Keyboard interactions, Focus navigation for keyboard, gamepad, remote control, and accessibility tools, Programmatic focus navigation