を使用しています。
WPF UI要素に、子コントロールのAutomationPeersを返さずにデフォルトの動作とは異なる動作をするカスタムAutomationPeerを使用させることで、問題を解決できる場合があります。これにより、UIオートメーションの作業は停止する可能性がありますが、うまくいけば私の場合はUIオートメーションを使用していません。
FrameworkElementAutomationPeer
を継承し、GetChildrenCore
メソッドをオーバーライドするカスタムオートメーションピアクラスを作成し、子コントロールオートメーションピアの代わりに空のリストを作成します。これにより、何かがAutomationPeersのツリーを反復しようとするときに問題が発生しなくなります。
さらに、オートメーションピアを使用するコントロールタイプを指定するには、GetAutomationControlTypeCore
を上書きします。この例では、コンストラクタパラメータとしてAutomationControlType
を渡しています。カスタムオートメーションピアをWindowsに適用すると、すべての子を返すためにルート要素が使用されると思うので、問題を解決するはずです。
public class MockAutomationPeer : FrameworkElementAutomationPeer
{
AutomationControlType _controlType;
public MockAutomationPeer(FrameworkElement owner, AutomationControlType controlType)
: base(owner)
{
_controlType = controlType;
}
protected override string GetNameCore()
{
return "MockAutomationPeer";
}
protected override AutomationControlType GetAutomationControlTypeCore()
{
return _controlType;
}
protected override List<AutomationPeer> GetChildrenCore()
{
return new List<AutomationPeer>();
}
}
カスタムオートメーションピアを使用するには、UIエレメントのOnCreateAutomationPeer
メソッドをオーバーライドします。ウィンドウ:
protected override System.Windows.Automation.Peers.AutomationPeer OnCreateAutomationPeer()
{
return new MockAutomationPeer(this, AutomationControlType.Window);
}
例外がスローされるコードの場所を表示できますか? – Terrance