在 UI 自动化提供程序中支持控件模式
备注
本文档适用于想要使用 System.Windows.Automation 命名空间中定义的托管 UI 自动化类的 .NET Framework 开发人员。 有关 UI 自动化的最新信息,请参阅 Windows 自动化 API:UI 自动化。
本主题演示如何在 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的实现。 大多数列表框控件还支持其他模式,但在本示例中对所有其他模式标识符返回了空引用(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