Share via


Supportare pattern di controllo in un provider di automazione interfaccia utente

Nota

Questa documentazione è destinata agli sviluppatori .NET Framework che desiderano utilizzare le classi di UI Automation gestite definite nello spazio dei nomi System.Windows.Automation. Per informazioni aggiornate su UI Automation, vedere API di automazione di Windows: UI Automation.

In questo argomento viene descritto come implementare uno o più pattern di controllo in un provider di automazione interfaccia utente in modo che le applicazioni client possano modificare i controlli e ottenere dati.

Supportare i pattern di controllo

  1. Implementare le interfacce appropriate per i pattern di controllo che l'elemento deve supportare, ad esempio IInvokeProvider per InvokePattern.

  2. Restituire l'oggetto che contiene l'implementazione di ciascuna interfaccia di controllo nell'implementazione di IRawElementProviderSimple.GetPatternProvider

Esempio 1

Nell'esempio seguente viene illustrata un'implementazione di ISelectionProvider per una casella di riepilogo personalizzata a selezione singola. Restituisce tre proprietà e ottiene l'elemento attualmente selezionato.

#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

Esempio 2

Nell'esempio seguente viene illustrata un'implementazione GetPatternProvider che restituisce la classe che implementa ISelectionProvider. La maggior parte dei controlli casella di riepilogo supporta anche altri pattern, ma in questo esempio viene restituito un riferimento null (Nothing in Visual Basic .NET di Microsoft) per tutti gli altri identificatori di pattern.

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

Vedi anche