12

Mircosoft UIAutomationフレームワークを使用してtextfield/textbox要素にテキストを設定したいのですが、これは、またはAutomationElementからのことを意味します。ControlType.EditControlType.Document

現在、私はTextPatternこれらのいずれかからテキストを取得するためにを使用していますAutomationElements

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

しかし今、私はに新しいテキストを設定したいと思いますAutomationElementTextPatternクラスでこのためのメソッドが見つかりません。だから私は使用しようとしていますが、ValuePatternそれが正しい方法かどうかはわかりません:

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

テキスト値を設定する他の方法はありますか?

Editもう1つの質問は、 /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");
}
4

1 に答える 1

13

あなたはValuePaternを使うことができます、それはそれをする方法です。私自身のコードから:

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) {...}

便利な(この場合のAutomationPropertyChangedEventHandler使用)もあります。Automation.AddAutomationPropertyChangedEventHandler(...)

MSDNのこのサンプルに基づいています。

于 2012-06-22T08:32:08.850 に答える