1

我正在使用 UIAutomation 做一些工作,需要在 WPF 中获取 AvalonEdit 控件的内容。我只能将 AvalonEdit 控件作为文本的 ControlType 来获取:

var editors = app.FindAll(TreeScope.Descendants, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Text));

不支持这个...

var targetTextPattern = editor[0].GetCurrentPattern( TextPattern.Pattern) as TextPattern;

我似乎找不到从中提取文本内容的方法,使用 ControlType.Text 时不可能吗?我也尝试过使用 ControlType Edit & Document 但 AvalonEdit 似乎不支持它们。

任何帮助都将不胜感激。谢谢!

4

1 回答 1

2

在对源代码进行了一些挖掘之后,我发现 AvalonEdit.TextEditor 确实支持 UIAutomation。这些是使用它所需的完整步骤。

首先,使用 ControlType.Custom 找到 TextEditor:

allEditors = app.FindAll(TreeScope.Descendants, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Custom));

TextEditorAutomationPeer 类实现了 IValueProvider,因此要使用 UIAutomation 从 TextEditor 获取文本,请像这样使用 ValuePattern:

var editorValuePattern = allEditors[0].GetCurrentPattern(ValuePattern.Pattern) as ValuePattern;
var text = editorValuePattern.Current.Value;

这对我有用:)

于 2011-07-18T20:27:28.503 回答