【问题标题】:Getting text from AvalonEdit with UIAutomation使用 UIAutomation 从 AvalonEdit 获取文本
【发布时间】:2011-07-17 08:03:49
【问题描述】:

我正在使用 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 似乎不支持它们。

感谢任何帮助。 谢谢!

【问题讨论】:

    标签: wpf ui-automation avalonedit


    【解决方案1】:

    在对源代码进行一些挖掘后,我发现 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;
    

    这对我有用:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-11-28
      • 1970-01-01
      • 1970-01-01
      • 2011-04-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多