UIElement.OnCreateAutomationPeer Method

Definition

When implemented in a derived class, returns class-specific AutomationPeer implementations for the Microsoft UI Automation infrastructure.

protected:
 virtual AutomationPeer ^ OnCreateAutomationPeer() = OnCreateAutomationPeer;
AutomationPeer OnCreateAutomationPeer();
protected virtual AutomationPeer OnCreateAutomationPeer();
function onCreateAutomationPeer()
Protected Overridable Function OnCreateAutomationPeer () As AutomationPeer

Returns

The class-specific AutomationPeer subclass to return.

Examples

The entirety of an OnCreateAutomationPeer implementation should consist of constructing the custom automation peer class and returning it.

protected override AutomationPeer OnCreateAutomationPeer() 
{
    return new MediaContainerAP(this, mediaElement); 
}
#include "MediaContainerAP.h"
...
public:
    MyNamespace::MediaContainerAP OnCreateAutomationPeer()
    {
        return winrt::make<MyNamespace::implementation::MediaContainerAP>(*this, mediaElement());
    }
protected:
    virtual AutomationPeer^ OnCreateAutomationPeer() override
    {
        return ref new MediaContainerAP(this, mediaElement);
    }
};
Protected Overrides Function OnCreateAutomationPeer() As AutomationPeer
    Return New MediaContainerAP(Me, mediaElement)
End Function

Remarks

For more info on the purpose of an automation peer and why you might need to define a class-specific AutomationPeer class, see Custom automation peers.

You should override this method in a custom class where you want to supply a custom automation peer for Microsoft UI Automation, rather than the default peer that is referenced by the default OnCreateAutomationPeer implementation. How you define a custom peer for your custom control depends on your control's accessibility requirements , its UI contract, and its behavior. For more info on why you might want to define a new peer, see Custom automation peers.

Note

This sample is not maintained and might not compile.

See XAML accessibility sample for an implementation of OnCreateAutomationPeer that defines the custom peer returned by OnCreateAutomationPeer (the peer implementation is part of Scenario 3 in this sample).

We recommend that the OnCreateAutomationPeer implementation should do nothing more than initialize a new instance of your custom automation peer, passing the calling control as owner, and return that instance. Do not attempt additional logic in this method. In particular, any logic that could potentially lead to destruction of the AutomationPeer within the same call may result in unexpected runtime behavior.

Applies to

See also