WPF 自动化触摸屏设备上的对等崩溃

本文关键字:对等 崩溃 自动化 触摸屏 WPF | 更新日期: 2023-09-27 18:37:03

我创建了一个WPF应用程序。它在台式机上完全可以正常工作,但是当应用程序在触摸屏上运行时,它就会崩溃。我已经关闭了触摸屏进程,应用程序工作正常。我想知道有没有人找到比禁用触摸屏进程"更好"的修复方法,因为这在微软 Surface 或 Windows 平板电脑上不起作用。

我目前正在使用 .Net 4.5

WPF 自动化触摸屏设备上的对等崩溃

我在使用 WPF AutomationPeer时也遇到了很多问题。

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

创建一个自定义自动化对等类,该类继承自FrameworkElementAutomationPeer并重写 GetChildrenCore 方法,以返回空列表而不是子控件自动化对等。这应该可以阻止在尝试遍历自动化对等树时出现问题。

还要重写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 元素(例如 Window)中覆盖 OnCreateAutomationPeer 方法:

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