다음을 통해 공유


UI 자동화 공급자의 컨트롤 패턴 지원

참고참고

이 문서는 System.Windows.Automation 네임스페이스에 정의된 관리되는 UI Automation 클래스를 사용하려는 .NET Framework 개발자를 위해 작성되었습니다.UI Automation에 대한 최신 정보는 Windows Automation API: UI Automation을 참조하십시오.

이 항목에서는 클라이언트 응용 프로그램으로 컨트롤을 조작하여 데이터를 가져올 수 있도록 UI 자동화 공급자에서 하나 이상의 컨트롤 패턴을 구현하는 방법을 보여 줍니다.

컨트롤 패턴 지원

  1. 요소가 지원해야 하는 컨트롤 패턴에 대한 적절한 인터페이스를 구현합니다. 예를 들어 InvokePattern에 대해 IInvokeProvider를 구현합니다.

  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

다음 예제에서는 ISelectionProvider를 구현하는 클래스를 반환하는 GetPatternProvider의 구현을 보여 줍니다. 대부분의 목록 상자 컨트롤은 다른 패턴도 지원하지만 이 예제에서는 다른 모든 패턴 식별자에 대해 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 자동화 공급자 구현