Browse Source

Added ControlAutomationPeer.FromElement.

pull/11523/head
Steven Kirk 3 years ago
parent
commit
966c5a0c5e
  1. 20
      src/Avalonia.Controls/Automation/Peers/ControlAutomationPeer.cs
  2. 6
      src/Avalonia.Controls/Control.cs

20
src/Avalonia.Controls/Automation/Peers/ControlAutomationPeer.cs

@ -31,11 +31,31 @@ namespace Avalonia.Automation.Peers
return CreatePeerForElement(element); return CreatePeerForElement(element);
} }
/// <summary>
/// Gets the <see cref="AutomationPeer"/> for a <see cref="Control"/>, creating it if
/// necessary.
/// </summary>
/// <param name="element">The control.</param>
/// <returns>The automation peer.</returns>
/// <remarks>
/// Despite the name (which comes from the analogous WPF API), this method does not create
/// a new peer if one already exists: instead it returns the existing peer.
/// </remarks>
public static AutomationPeer CreatePeerForElement(Control element) public static AutomationPeer CreatePeerForElement(Control element)
{ {
return element.GetOrCreateAutomationPeer(); return element.GetOrCreateAutomationPeer();
} }
/// <summary>
/// Gets an existing <see cref="AutomationPeer"/> for a <see cref="Control"/>.
/// </summary>
/// <param name="element">The control.</param>
/// <returns>The automation peer if already created; otherwise null.</returns>
/// <remarks>
/// To ensure that a peer is created, use <see cref="CreatePeerForElement(Control)"/>.
/// </remarks>
public static AutomationPeer? FromElement(Control element) => element.GetAutomationPeer();
protected override void BringIntoViewCore() => Owner.BringIntoView(); protected override void BringIntoViewCore() => Owner.BringIntoView();
protected override IReadOnlyList<AutomationPeer> GetOrCreateChildrenCore() protected override IReadOnlyList<AutomationPeer> GetOrCreateChildrenCore()

6
src/Avalonia.Controls/Control.cs

@ -440,6 +440,12 @@ namespace Avalonia.Controls
return new NoneAutomationPeer(this); return new NoneAutomationPeer(this);
} }
internal AutomationPeer? GetAutomationPeer()
{
VerifyAccess();
return _automationPeer;
}
internal AutomationPeer GetOrCreateAutomationPeer() internal AutomationPeer GetOrCreateAutomationPeer()
{ {
VerifyAccess(); VerifyAccess();

Loading…
Cancel
Save