-api-id | -api-type |
---|---|
M:Windows.UI.Xaml.Input.FocusManager.TryFocusAsync(Windows.UI.Xaml.DependencyObject,Windows.UI.Xaml.FocusState) |
winrt method |
Asynchronously attempts to set focus on an element when the application is initialized.
The object on which to set focus.
One of the values from the FocusState enumeration that specify how an element can obtain focus.
The FocusMovementResult that indicates whether focus was successfully set.
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.
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);
}
}
Keyboard interactions, Focus navigation for keyboard, gamepad, remote control, and accessibility tools, Programmatic focus navigation