English 中文(简体)
在带有自动框架的文本字段/文本框中设置文本,然后获取更改事件
原标题:set text on textfield / textbox with the automation framework and get the change event

我想在文本字段/文本框元素上设置一个文本字段/文本框元素的文本,并使用MircoSoft UI Automation 框架,这意味着在 Automation Eplement ControlType.Edit ControlType.Document 的 < document 上设置一个文本字段/文本框元素。

i m 使用 < code> TextPatter 获取这些 < code> 自动元素之一的文字 < /code> :

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

但现在我想在 自动元素 中设置新文本。 在 < code> TextPater 类中找不到新的方法。 所以我尝试使用 < code> ValuePater 但我不确定这样做是否正确 :

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");
}
最佳回答

你可以使用"价值父亲",这是做它的方式。从我自己的代码:

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

您可以注册到 Event , 使用:

var myEventHandler= 
            new AutomationEventHandler(handler);

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

handler 方法:

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

还有一个 < code> 自动变换EventHandler (使用 < code> Automation.AddAutomation property ChangedEventHandler (...) , 在本案中) 有用 。

基于此sample 来自MSDN。

问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签