共用方式為


支援 UI 自動化提供者的控制項模式

注意事項注意事項

這份文件適用於想要使用 System.Windows.Automation 命名空間中定義之 Managed UI Automation 類別的 .NET Framework 開發人員。如需 UI Automation 的最新資訊,請參閱 Windows Automation API:使用者介面自動化 (英文)。

本主題說明如何在 UI 自動化提供者上實作一個或多個控制項模式,讓用戶端應用程式能夠操作控制項並從中取得資料。

支援控制項模式

  1. 針對項目應該支援的控制項模式,實作適當的介面,例如 IInvokeProvider 適用於 InvokePattern

  2. IRawElementProviderSimple.GetPatternProvider 的實作中,傳回包含每個控制項介面實作的物件。

範例

下列範例示範單一選取自訂清單方塊的 ISelectionProvider 實作。 它會傳回三個屬性並取得目前選取的項目。

#Region "ISelectionProvider Members"

''' <summary>
''' Specifies whether selection of more than one item at a time is supported.
''' </summary>

Public ReadOnly Property CanSelectMultiple() As Boolean _
    Implements ISelectionProvider.CanSelectMultiple
    Get
        Return False
    End Get
End Property
''' <summary>
''' Specifies whether the list has to have an item selected at all times.
''' </summary>

Public ReadOnly Property IsSelectionRequired() As Boolean _
    Implements ISelectionProvider.IsSelectionRequired
    Get
        Return True
    End Get
End Property

''' <summary>
''' Returns the automation provider for the selected list item.
''' </summary>
''' <returns>The selected item.</returns>
''' <remarks>
''' MyList is an ArrayList collection of providers for items in the list box.
''' SelectedIndex is the index of the selected item.
''' </remarks>
Public Function GetSelection() As IRawElementProviderSimple() _
    Implements ISelectionProvider.GetSelection
    If SelectedIndex >= 0 Then
        Dim itemProvider As IRawElementProviderSimple = DirectCast(MyList(SelectedIndex), IRawElementProviderSimple)
        Dim providers(1) As IRawElementProviderSimple
        providers(0) = itemProvider
        Return providers
    Else
        Return Nothing
    End If

End Function 'GetSelection 
#End Region
Private Members As ISelectionProvider
#region ISelectionProvider Members

/// <summary>
/// Specifies whether selection of more than one item at a time is supported.
/// </summary>
public bool CanSelectMultiple
{
    get
    {
        return false;
    }
}

/// <summary>
/// Specifies whether the list has to have an item selected at all times.
/// </summary>
public bool IsSelectionRequired
{
    get
    {
        return true;
    }
}

/// <summary>
/// Returns the automation provider for the selected list item.
/// </summary>
/// <returns>The selected item.</returns>
/// <remarks>
/// MyList is an ArrayList collection of providers for items in the list box.
/// SelectedIndex is the index of the selected item.
/// </remarks>
public IRawElementProviderSimple[] GetSelection()
{
    if (SelectedIndex >= 0)
    {
        IRawElementProviderSimple itemProvider = (IRawElementProviderSimple)MyList[SelectedIndex];
        IRawElementProviderSimple[] providers =  { itemProvider };
        return providers;
    }
    else return null;
}
#endregion ISelectionProvider Members

下列範例示範 GetPatternProvider 的實作,它會傳回實作 ISelectionProvider 的類別。 大部分清單方塊也會支援其他模式,但在本範例中會針對所有其他模式識別項傳回 null 參考 (在 Microsoft Visual Basic .NET 中為 Nothing)。

''' <summary>
''' Returns the object that supports the specified pattern.
''' </summary>
''' <param name="patternId">ID of the pattern.</param>
''' <returns>Object that implements IInvokeProvider.</returns>
''' <remarks>
''' In this case, the ISelectionProvider interface is implemented in another provider-defined class, 
''' ListPattern. However, it could be implemented in the base provider class, in which case the 
''' method would simply return "this".
''' </remarks>
Function GetPatternProvider(ByVal patternId As Integer) As Object _
    Implements IRawElementProviderSimple.GetPatternProvider

    If patternId = SelectionPatternIdentifiers.Pattern.Id Then
        Return New ListPattern(myItems, SelectedIndex)
    Else
        Return Nothing
    End If

End Function 'IRawElementProviderSimple.GetPatternProvider
/// <summary>
/// Returns the object that supports the specified pattern.
/// </summary>
/// <param name="patternId">ID of the pattern.</param>
/// <returns>Object that implements IInvokeProvider.</returns>
/// <remarks>
/// In this case, the ISelectionProvider interface is implemented in another provider-defined class, 
/// ListPattern. However, it could be implemented in the base provider class, in which case the 
/// method would simply return "this".
/// </remarks>
object IRawElementProviderSimple.GetPatternProvider(int patternId)
{
    if (patternId == SelectionPatternIdentifiers.Pattern.Id)
    {
        return new ListPattern(myItems, SelectedIndex);
    }
    else
    {
        return null;
    }
}

請參閱

概念

UI 自動化提供者概觀

伺服器端 UI 自動化提供者實作