UIElement.OnCreateAutomationPeer UIElement.OnCreateAutomationPeer UIElement.OnCreateAutomationPeer UIElement.OnCreateAutomationPeer Method


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

protected : virtual AutomationPeer OnCreateAutomationPeer()
virtual AutomationPeer OnCreateAutomationPeer() const;
protected virtual AutomationPeer OnCreateAutomationPeer()
Protected Overridable Function OnCreateAutomationPeer() As AutomationPeer

The class-specific AutomationPeer subclass to return.


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"
    MyNamespace::MediaContainerAP OnCreateAutomationPeer()
        return winrt::make<MyNamespace::implementation::MediaContainerAP>(*this, mediaElement());
    virtual AutomationPeer^ OnCreateAutomationPeer() override
        return ref new MediaContainerAP(this, mediaElement);
Protected Overrides Function OnCreateAutomationPeer() As AutomationPeer
    Return New MediaContainerAP(Me, mediaElement)
End Function


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. To see a sample that implements OnCreateAutomationPeer and defines the custom peer that OnCreateAutomationPeer returns, see XAML accessibility sample (the peer implementation is part of Scenario 3 in that 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.

See Also