Podpora vzorů ovládacích prvků u zprostředkovatele automatizace uživatelského rozhraní

Poznámka:

Tato dokumentace je určená pro vývojáře rozhraní .NET Framework, kteří chtějí používat spravované třídy model UI Automation definované v System.Windows.Automation oboru názvů. Nejnovější informace o model UI Automation najdete v tématu Rozhraní API služby Windows Automation: model UI Automation.

Toto téma ukazuje, jak implementovat jeden nebo více vzorů ovládacích prvků u poskytovatele model UI Automation, aby klientské aplikace mohly manipulovat s ovládacími prvky a získávat z nich data.

Podpora vzorů ovládacích prvků

  1. Implementujte příslušná rozhraní pro řídicí vzory, které by měl prvek podporovat, například IInvokeProvider pro InvokePattern.

  2. Vrácení objektu obsahujícího vaši implementaci každého řídicího rozhraní v implementaci IRawElementProviderSimple.GetPatternProvider

Příklad 1

Následující příklad ukazuje implementaci vlastního ISelectionProvider seznamu s jedním výběrem. Vrátí tři vlastnosti a získá aktuálně vybranou položku.

#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

Příklad 2

Následující příklad ukazuje implementaci GetPatternProvider , která vrací třídu implementovat ISelectionProvider. Většina ovládacíchprvkůchm prvkům je podporována i jiná schémata, ale v tomto příkladu se vrátí nulový odkaz (Nothing v jazyce Microsoft Visual Basic .NET) pro všechny ostatní identifikátory vzorů.

/// <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

Viz také