支援 UI 自動化提供者的控制項模式
注意
本文件適用對象為 .NET Framework 開發人員,其想要使用 System.Windows.Automation 命名空間中定義的受控 UI 自動化類別。 如需 UI 自動化的最新資訊,請參閱 Windows 自動化 API:UI 自動化。
本主題說明如何在使用者介面自動化提供者上實作一個或多個控制項模式,讓用戶端應用程式能夠操作控制項並從中取得資料。
支援控制項模式
針對項目應該支援的控制項模式,實作適當的介面,例如適用於 IInvokeProvider 的 InvokePattern。
在 IRawElementProviderSimple.GetPatternProvider實作中,傳回包含每個控制項介面實作的物件
範例 1
下列範例示範單一選取自訂清單方塊的 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
#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
範例 2
下列範例示範 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>
object IRawElementProviderSimple.GetPatternProvider(int patternId)
{
if (patternId == SelectionPatternIdentifiers.Pattern.Id)
{
return new ListPattern(myItems, SelectedIndex);
}
else
{
return null;
}
}
''' <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