Can UI Automation be disabled for an entire WPF 4.0 app?

前端 未结 5 1160
死守一世寂寞
死守一世寂寞 2021-01-30 09:47

We are developing a WPF 4.0 application for internal use.
On some clients, we are experiencing huge performance issues due to UI automation (these clients have software i

5条回答
  •  粉色の甜心
    2021-01-30 09:47

    We hit the exact same issue mentioned in the question, where a UI automation client was impacting the performance of our WPF application.

    After trying all the hot fixes and workarounds, finally we found a solution. Each UI control has an AutomationPeer object that exposes the properties of the current control and its child controls. The UI automation client uses these AutomationPeer objects to get the information about the UI controls. There are built-in automation peer class for most of the UI controls in WPF and we can also create a custom peer class.

    The following is a custom automation peer class. Note that in the GetChildrenCore method, it is returning an empty list instead of the list of actual child controls.

    public class CustomWindowAutomationPeer : FrameworkElementAutomationPeer
    {
        public CustomWindowAutomationPeer(FrameworkElement owner) : base(owner) { }
    
        protected override string GetNameCore()
        {
            return "CustomWindowAutomationPeer";
        }
    
        protected override AutomationControlType GetAutomationControlTypeCore()
        {
            return AutomationControlType.Window;
        }
    
        protected override List GetChildrenCore()
        {
            return new List();
        }
    }
    

    Then in your main window, override the OnCreateAutomationPeer method:

    protected override System.Windows.Automation.Peers.AutomationPeer OnCreateAutomationPeer()
    {
        return new CustomWindowAutomationPeer(this);
    }
    

    Now when the UI automation client tries to get the child controls of the main window, it gets back an empty list and so it cannot iterate through the rest of the controls.

    Refer this MSDN article for more details.

提交回复
热议问题