我想使用 Mircosoft UI 自动化框架在文本字段/文本框元素上设置文本,这意味着AutomationElement来自ControlType.Edit或ControlType.Document.
目前我正在使用TextPattern从其中之一获取文本AutomationElements:
TextPattern tp = (TextPattern)element.GetCurrentPattern(TextPattern.Pattern);
string text = tp.DocumentRange.GetText(-1).Trim();
但现在我想在AutomationElement. TextPattern我在课堂上找不到这种方法。所以我正在尝试使用,ValuePattern但我不确定这是否是正确的方法:
ValuePattern value = element.GetCurrentPattern(ValuePattern.Pattern) as ValuePattern;
value.SetValue(insertText);
有没有其他方法来设置文本值?
另一个问题是如何在Edit/Document元素上更改文本时获取事件?我尝试使用,TextChangedEvent但更改文本时没有触发任何事件:
AutomationEventHandler ehTextChanged = new AutomationEventHandler(text_event);
Automation.AddAutomationEventHandler(TextPattern.TextChangedEvent, element, TreeScope.Element, ehTextChanged);
private void text_event(object sender, AutomationEventArgs e)
{
Console.WriteLine("Text changed");
}