Crear un proveedor de UI Automation en el clienteCreate a Client-Side UI Automation Provider

Nota

Esta documentación está dirigida a los desarrolladores de .NET Framework que quieran usar las clases Automatización de la interfaz de usuarioUI Automation administradas definidas en el espacio de nombres System.Windows.Automation.This documentation is intended for .NET Framework developers who want to use the managed Automatización de la interfaz de usuarioUI Automation classes defined in the System.Windows.Automation namespace. Para ver la información más reciente acerca de Automatización de la interfaz de usuarioUI Automation, consulte Windows Automation API: automatización de la interfaz de usuario.For the latest information about Automatización de la interfaz de usuarioUI Automation, see Windows Automation API: UI Automation.

Este tema contiene código de ejemplo que muestra cómo implementar un proveedor de Automatización de la interfaz de usuario del lado cliente.This topic contains example code that shows how to implement a client-side UI Automation provider.

EjemploExample

El ejemplo de código siguiente se puede compilar en un archivo biblioteca de vínculos dinámicos (DLL)dynamic-link library (DLL) que implemente un proveedor de cliente muy simple para una ventana de consola.The following example code can be built into a biblioteca de vínculos dinámicos (DLL)dynamic-link library (DLL) that implements a very simple client-side provider for a console window. El código no tiene ninguna funcionalidad práctica, pero está pensado para mostrar los pasos básicos para configurar un ensamblado de proveedor que se pueda registrar mediante una aplicación de cliente de Automatización de la interfaz de usuario.The code does not have any useful functionality, but is intended to demonstrate the basic steps in setting up a provider assembly that can be registered by a UI Automation client application.

using System;
using System.Windows.Automation;
using System.Windows.Automation.Provider;

namespace ClientSideProviderAssembly
{
    // The assembly must implement a UIAutomationClientSideProviders class, 
    // and the namespace must be the same as the name of the DLL, so that 
    // UI Automation can find the table of descriptors. In this example, 
    // the DLL would be "ClientSideProviderAssembly.dll"

    static class UIAutomationClientSideProviders
    {
        /// <summary>
        /// Implementation of the static ClientSideProviderDescriptionTable field. 
        /// In this case, only a single provider is listed in the table.
        /// </summary>
        public static ClientSideProviderDescription[] ClientSideProviderDescriptionTable = 
            { 
                new ClientSideProviderDescription(
                    // Method that creates the provider object.
                    new ClientSideProviderFactoryCallback(ConsoleProvider.Create),   
                    // Class of window that will be served by the provider.
                    "ConsoleWindowClass")                                                
            };
    }

    class ConsoleProvider : IRawElementProviderSimple
    {
        IntPtr providerHwnd;

        public ConsoleProvider(IntPtr hwnd)
        {
            providerHwnd = hwnd;
        }

        internal static IRawElementProviderSimple Create(
            IntPtr hwnd, int idChild, int idObject)
        {
            // This provider doesn't expose children, so never expects 
            // nonzero values for idChild.
            if (idChild != 0)
                return null;
            else
                return new ConsoleProvider(hwnd);

        }

        private static IRawElementProviderSimple Create(
            IntPtr hwnd, int idChild)
        {
            // Something is wrong if idChild is not 0.
            if (idChild != 0) return null;
            else return new ConsoleProvider(hwnd);
        }

        #region IRawElementProviderSimple

        // This is a skeleton implementation. The only real functionality 
        // at this stage is to return the name of the element and the host 
        // window provider, which can supply other properties.

        ProviderOptions IRawElementProviderSimple.ProviderOptions
        {
            get
            {
                return ProviderOptions.ClientSideProvider;
            }
        }

        IRawElementProviderSimple IRawElementProviderSimple.HostRawElementProvider
        {
            get
            {
                return AutomationInteropProvider.HostProviderFromHandle(providerHwnd);
            }
        }

        object IRawElementProviderSimple.GetPropertyValue(int propertyId)
        {
            if (propertyId == AutomationElementIdentifiers.NameProperty.Id)
                return "Custom Console Window";
            else
                return null;

        }

        object IRawElementProviderSimple.GetPatternProvider(int iid)
        {
            return null;
        }
        #endregion IRawElementProviderSimple
    }
}

Imports System
Imports System.Windows.Automation
Imports System.Windows.Automation.Provider

Namespace ClientSideProviderAssembly
	' The assembly must implement a UIAutomationClientSideProviders class, 
	' and the namespace must be the same as the name of the DLL, so that 
	' UI Automation can find the table of descriptors. In this example, 
	' the DLL would be "ClientSideProviderAssembly.dll"

	Friend NotInheritable Class UIAutomationClientSideProviders
		''' <summary>
		''' Implementation of the static ClientSideProviderDescriptionTable field. 
		''' In this case, only a single provider is listed in the table.
		''' </summary>
		Public Shared ClientSideProviderDescriptionTable() As ClientSideProviderDescription = { New ClientSideProviderDescription(New ClientSideProviderFactoryCallback(AddressOf ConsoleProvider.Create), "ConsoleWindowClass") }
					' Method that creates the provider object.
					' Class of window that will be served by the provider.
	End Class

	Friend Class ConsoleProvider
		Implements IRawElementProviderSimple
		Private providerHwnd As IntPtr

		Public Sub New(ByVal hwnd As IntPtr)
			providerHwnd = hwnd
		End Sub

		Friend Shared Function Create(ByVal hwnd As IntPtr, ByVal idChild As Integer, ByVal idObject As Integer) As IRawElementProviderSimple
			' This provider doesn't expose children, so never expects 
			' nonzero values for idChild.
			If idChild <> 0 Then
				Return Nothing
			Else
				Return New ConsoleProvider(hwnd)
			End If

		End Function

		Private Shared Function Create(ByVal hwnd As IntPtr, ByVal idChild As Integer) As IRawElementProviderSimple
			' Something is wrong if idChild is not 0.
			If idChild <> 0 Then
				Return Nothing
			Else
				Return New ConsoleProvider(hwnd)
			End If
		End Function

		#Region "IRawElementProviderSimple"

		' This is a skeleton implementation. The only real functionality 
		' at this stage is to return the name of the element and the host 
		' window provider, which can supply other properties.

		Private ReadOnly Property IRawElementProviderSimple_ProviderOptions() As ProviderOptions Implements IRawElementProviderSimple.ProviderOptions
			Get
				Return ProviderOptions.ClientSideProvider
			End Get
		End Property

		Private ReadOnly Property HostRawElementProvider() As IRawElementProviderSimple Implements IRawElementProviderSimple.HostRawElementProvider
			Get
				Return AutomationInteropProvider.HostProviderFromHandle(providerHwnd)
			End Get
		End Property

		Private Function GetPropertyValue(ByVal propertyId As Integer) As Object Implements IRawElementProviderSimple.GetPropertyValue
			If propertyId = AutomationElementIdentifiers.NameProperty.Id Then
				Return "Custom Console Window"
			Else
				Return Nothing
			End If

		End Function

		Private Function GetPatternProvider(ByVal iid As Integer) As Object Implements IRawElementProviderSimple.GetPatternProvider
			Return Nothing
		End Function
		#End Region ' IRawElementProviderSimple
	End Class
End Namespace

Vea tambiénSee Also

Información general sobre proveedores de la Automatización de la interfaz de usuarioUI Automation Providers Overview
Registro de un ensamblado de proveedor del lado clienteRegister a Client-Side Provider Assembly