Skip to content

Latest commit

 

History

History
69 lines (45 loc) · 3.02 KB

focusmanager_tryfocusasync_1779533284.md

File metadata and controls

69 lines (45 loc) · 3.02 KB
-api-id -api-type
M:Windows.UI.Xaml.Input.FocusManager.TryFocusAsync(Windows.UI.Xaml.DependencyObject,Windows.UI.Xaml.FocusState)
winrt method

Windows.UI.Xaml.Input.FocusManager.TryFocusAsync

-description

Asynchronously attempts to set focus on an element when the application is initialized.

-parameters

-param element

The object on which to set focus.

-param value

One of the values from the FocusState enumeration that specify how an element can obtain focus.

-returns

The FocusMovementResult that indicates whether focus was successfully set.

-remarks

Some objects, such as a WebView, can run in either the app process or in a separate process (see WebViewExecutionMode.SeparateProcess).

When an object runs in the app process, the following focus events occur as expected for both the previously focused object and the newly focused object:

However, if the newly focused object is running in a separate process some of these event behaviors can differ.

  • GetFocusedElement does not return the newly focused object until the TryFocusAsync 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 receieves 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.

-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

Keyboard interactions, Focus navigation for keyboard, gamepad, remote control, and accessibility tools, Programmatic focus navigation