gpt4 book ai didi

c# - 触摸屏设备上的 WPF AutomationPeer 崩溃

转载 作者:太空狗 更新时间:2023-10-29 17:55:58 25 4
gpt4 key购买 nike

我创建了一个 WPF 应用程序。它在台式机上工作得很好,但当应用程序在触摸屏上运行时它崩溃了。我关闭了触摸屏进程,应用程序运行良好。我想知道有没有人找到比禁用触摸屏进程“更好”的修复方法,因为这在 Microsoft Surface 或 Windows 平板电脑上不起作用。

我目前使用的是 .Net 4.5

最佳答案

我在使用 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>();
}
}

要使用自定义自动化对等点,请覆盖 UI 元素中的 OnCreateAutomationPeer 方法,例如窗口:

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

关于c# - 触摸屏设备上的 WPF AutomationPeer 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35727592/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com