WPF AutomationPeer TouchScreen设备崩溃

Mas*_*ter 9 c# visual-studio

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

我目前正在使用.Net 4.5

Gle*_*mas 7

我也有很多 WPF 问题AutomationPeer

您可以通过强制 WPF UI 元素使用自定义 AutomationPeer 来解决您的问题,该自定义 AutomationPeer 通过不返回子控件的 AutomationPeers 与默认的行为不同。这可能会阻止任何 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>();
    }
}
Run Code Online (Sandbox Code Playgroud)

要使用自定义自动化对等点覆盖OnCreateAutomationPeerUI 元素中的方法,例如 Window:

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