设置文本框/文本框的文字与自动化框架,并得到改变事件 [英] set text on textfield / textbox with the automation framework and get the change event

查看:387
本文介绍了设置文本框/文本框的文字与自动化框架,并得到改变事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想设置一个文本框/文本框元素的文字的Mircosoft的 UI自动化的框架,即从一个 AutomationElement 表示 ControlType.Edit ControlType.Document



在那一刻我使用了的Textpattern 从其中的一个获取文本 AutomationElements

 的Textpattern TP =(textpattern的)element.GetCurrentPattern(TextPattern.Pattern); 
字符串文本= tp.DocumentRange.GetText(-1).Trim();



但现在我想设置在 AutomationElement 。我找不到在此方法的Textpattern 类。所以我想使用 ValuePattern ,但我不知道这是应该做的正确方法:

  ValuePattern值= element.GetCurrentPattern(ValuePattern.Pattern)为ValuePattern; 
value.SetValue(insertText);



是否有其他的方式来设置文本的价值?



这是另一个问题是我怎样才能得到一个事件时,该文本是在修改 / 文件更改元素?我试图使用 TextChangedEvent ,但我没有得到任何事件改变了文本时触发:

  AutomationEventHandler ehTextChanged =新AutomationEventHandler(text_event); 
Automation.AddAutomationEventHandler(TextPattern.TextChangedEvent,元件,TreeScope.Element,ehTextChanged);

私人无效text_event(对象发件人,AutomationEventArgs E)
{
Console.WriteLine(文本改变);
}


解决方案

您可以使用ValuePatern,它做到这一点的方式。从我自己的代码:

  ValuePattern ETB = EditableTextBox.GetCurrentPattern(ValuePattern.Pattern)为ValuePattern; 
etb.SetValue(测试);

您可以为事件使用寄存器:

  VAR一个MyEventHandler = 
新AutomationEventHandler(处理);

Automation.AddAutomationEventHandler(
SelectionItemPattern.ElementSelectedEvent,//在你的情况,你可能想使用另一种模式
targetApp,
TreeScope.Descendants,
一个MyEventHandler);



以及处理程序方法:

 私人无效处理程序(SRC对象,AutomationEventArgs E){...} 

还有一个 AutomationPropertyChangedEventHandler (使用 Automation.AddAutomationPropertyChangedEventHandler(...)在这种情况下),可以是有用的。



在此基础上的从MSDN样本


I want to set a text on a textfield / textbox element with the Mircosoft UI Automation framework, that means on a AutomationElement from the ControlType.Edit or ControlType.Document.

At the moment i'm using the TextPattern to get the text from one of these AutomationElements:

TextPattern tp = (TextPattern)element.GetCurrentPattern(TextPattern.Pattern);
string text = tp.DocumentRange.GetText(-1).Trim();

But now I want to set a new text in the AutomationElement. I can't find a method for this in the TextPattern class. So I'm trying to use the ValuePattern but I'm not sure if that's the right way to do it:

ValuePattern value = element.GetCurrentPattern(ValuePattern.Pattern) as ValuePattern;
value.SetValue(insertText);

Is there an other way to set the text value?

An other question is how can I get an event when the text was changed on a Edit / Document element? I tried to use the TextChangedEvent but i don't get any events fired when changing the text:

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");
}

解决方案

You can use the ValuePatern, it's the way to do it. From my own code :

ValuePattern etb = EditableTextBox.GetCurrentPattern(ValuePattern.Pattern) as ValuePattern;
etb.SetValue("test");

You can register to Event using:

var myEventHandler= 
            new AutomationEventHandler(handler);

Automation.AddAutomationEventHandler(
    SelectionItemPattern.ElementSelectedEvent, // In your case you might want to use another pattern
    targetApp, 
    TreeScope.Descendants, 
    myEventHandler);

And the handler method:

private void handler(object src, AutomationEventArgs e) {...}

There is also an AutomationPropertyChangedEventHandler (use Automation.AddAutomationPropertyChangedEventHandler(...) in this case) that can be useful.

Based on this sample from MSDN.

这篇关于设置文本框/文本框的文字与自动化框架,并得到改变事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆