FocusManager.TryFocusAsync(DependencyObject, FocusState) 方法

定義

在初始化應用程式時,以非同步方式嘗試將焦點設定在專案上。

public:
 static IAsyncOperation<FocusMovementResult ^> ^ TryFocusAsync(DependencyObject ^ element, FocusState value);
/// [Windows.Foundation.Metadata.RemoteAsync]
 static IAsyncOperation<FocusMovementResult> TryFocusAsync(DependencyObject const& element, FocusState const& value);
[Windows.Foundation.Metadata.RemoteAsync]
public static IAsyncOperation<FocusMovementResult> TryFocusAsync(DependencyObject element, FocusState value);
function tryFocusAsync(element, value)
Public Shared Function TryFocusAsync (element As DependencyObject, value As FocusState) As IAsyncOperation(Of FocusMovementResult)

參數

element
DependencyObject

要設定焦點的物件。

value
FocusState

FocusState列舉中的其中一個值,指定專案如何取得焦點。

傳回

FocusMovementResult,指出是否已成功設定焦點。

屬性

Windows 需求

裝置系列
Windows 10, version 1803 (已於 10.0.17134.0 引進)
API contract
Windows.Foundation.UniversalApiContract (已於 v6.0 引進)

範例

在這裡,我們會示範如何將焦點設定在 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); 
    } 
}

備註

某些物件,例如 WebView,可以在應用程式進程或個別進程中執行, (請參閱 WebViewExecutionMode.SeparateProcess) 。

當物件在應用程式進程中執行時,先前焦點物件和新焦點物件都會如預期般發生下列焦點事件:

不過,如果新焦點物件是在個別的進程中執行,其中有些事件行為可能會有所不同。

TryFocusAsync 會在應用程式進程中執行的元素上呼叫時同步完成。

適用於

另請參閱