WPF AutomationPeer TouchScreen设备崩溃

我创建了一个WPF应用程序。 它在桌面上完全正常,但是当应用程序在触摸屏上运行时,它会崩溃。 我已经关闭了触摸屏流程,应用程序运行完全正常。 我想知道有没有人找到一个“更好”的解决方案,而不是禁用触摸屏过程,因为这不适用于微软表面或Windows平板电脑。

我目前正在使用.Net 4.5

WPF AutomationPeer也遇到了很多问题。

您可以通过强制WPF UI元素使用自定义AutomationPeer来解决您的问题,该自定义AutomationPeer的行为与默认值不同,方法是不返回子控件的AutomationPeers。 这可能会阻止任何UI自动化工作,但希望在您的情况下,就像在我的情况下,您没有使用UI自动化..

创建一个inheritance自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 GetChildrenCore() { return new List(); } } 

要使用自定义自动化同级,请覆盖UI元素中的OnCreateAutomationPeer方法,例如Window:

 protected override System.Windows.Automation.Peers.AutomationPeer OnCreateAutomationPeer() { return new MockAutomationPeer(this, AutomationControlType.Window); }