FocusManager.TryMoveFocusAsync 方法

定义

重载

TryMoveFocusAsync(FocusNavigationDirection)

异步尝试将焦点从具有焦点的当前元素更改为指定方向的下一个可聚焦元素。

TryMoveFocusAsync(FocusNavigationDirection, FindNextElementOptions)

异步尝试将焦点从具有焦点的当前元素更改为指定方向的下一个可聚焦元素,并受制于指定的导航选项。

TryMoveFocusAsync(FocusNavigationDirection)

异步尝试将焦点从具有焦点的当前元素更改为指定方向的下一个可聚焦元素。

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)

参数

focusNavigationDirection
FocusNavigationDirection

焦点在应用 UI 中从元素移动到元素的方向。

返回

指示是否已成功设置焦点的 FocusMovementResult

属性

示例

此处,我们演示如何在 WebView 对象上设置焦点,如果失败,请将焦点还原到原始元素。

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

注解

在应用进程中运行的元素上调用时,TryFocusAsync 以同步方式完成。

还可以使用 FindNextElement (FocusNavigationDirection、FindNextElementOptions) 方法或 FindNextElement (FocusNavigationDirection) 方法以编程方式移动焦点。 这些方法检索元素 (为 DependencyObject) ,该元素将仅根据指定导航方向接收焦点 (方向,不能用于模拟选项卡导航) 。

注意

建议使用 FindNextElement 方法而不是 FindNextFocusableElement ,因为 FindNextFocusableElement 检索 ,如果下一个UIElementUIElement可聚焦元素不是 ((如) Hyperlink 对象),则返回 null

另请参阅

适用于

TryMoveFocusAsync(FocusNavigationDirection, FindNextElementOptions)

异步尝试将焦点从具有焦点的当前元素更改为指定方向的下一个可聚焦元素,并受制于指定的导航选项。

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)

参数

focusNavigationDirection
FocusNavigationDirection

焦点在应用 UI 中从元素移动到元素的方向。

focusNavigationOptions
FindNextElementOptions

用于标识候选焦点的导航选项。

返回

指示是否已成功设置焦点的 FocusMovementResult

属性

示例

此处,我们演示如何在 WebView 对象上设置焦点,如果失败,请将焦点还原到原始元素。

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

注解

在应用进程中运行的元素上调用时,TryFocusAsync 以同步方式完成。

还可以使用 FindNextElement (FocusNavigationDirection、FindNextElementOptions) 方法或 FindNextElement (FocusNavigationDirection) 方法以编程方式移动焦点。 这些方法检索元素 (为 DependencyObject) ,该元素将仅根据指定导航方向接收焦点 (方向,不能用于模拟选项卡导航) 。

注意

建议使用 FindNextElement 方法而不是 FindNextFocusableElement ,因为 FindNextFocusableElement 检索 ,如果下一个UIElementUIElement可聚焦元素不是 ((如) Hyperlink 对象),则返回 null

另请参阅

适用于