0

Silverlight xap をホストする asp.net ページにオブジェクトがあります (私の特定のケースでは IFrame にありますが、通常のオブジェクトにも興味があります)。UI Spy で要素を見つけることができますが、名前は "Silverlight Control" とだけ表示されています。自動テストで AutomationElement を見つけようとしても失敗します (コントロールは毎回 null です)。Silverlight コードまたは html に役立つ設定はありますか? 同じページに複数の Silverlight コントロールがある場合、どうすれば区別できますか?

<object id="silverlightClient" style="display:none;" data="data:application/x-silverlight-2," type="application/x-silverlight-2">
    <param name="source" value="../../ClientBin/SilverlightApplication.xap"/>
    <param name="onerror" value="onSilverlightError" />
    <param name="background" value="#00000000" /> 
    <param name="minRuntimeVersion" value="4.0.41019.0" />
    <param name="autoUpgrade" value="true" />
    <param name="windowless" value="false" />
</object>

   TreeWalker tw = new TreeWalker(new System.Windows.Automation.PropertyCondition(AutomationElement.NameProperty, "Silverlight Control));
   AutomationElement control = tw.GetFirstChild(ancestor);

UIスパイ

Identification
    ClassName: "MicrosoftSilverlight"
    ControlType: "ControlType.Window"
    Culture: "(null)"
    AutomationId: "71857844"
    LocalizedControlType: "window"
    Name: "Silverlight Control"
    ProcessId: "7636 (iexplore)"
    RuntimeId: "42 2163886"
    IsPassword: "False"
    IsControlElement: "True"
    IsContentElement: "True"

編集:画像を追加しました。オブジェクトが IFrame 内にあることにも気付きました。 UISpyImage - タイトル名が削除されました

4

1 に答える 1

0

AutomationElement の操作をいくらか簡単にするために、いくつかの拡張メソッドを作成しました。関連するものを以下に貼り付けましたが、詳細についてはこちらをご覧ください。

ルート IE ウィンドウへの参照があると仮定しています。そうでない場合でも、プロセス ID であることがわかっている場合は、次のように見つけることができます。

var ieWindow = AutomationElement.RootElement.FindChildByCondition(new PropertyCondition(AutomationElement.ProcessIdProperty, ieProcessId));

IE で開いているフレームが 1 つだけで、その上に Silverlight コントロールが 1 つあると仮定すると、次のことができます。

var silverlightControl = ieWindow.FindDescendentByClassPath(
                         new[]{
                               "Frame Tab",
                                 "TabWindowClass",
                                   "Shell DocObject View",
                                     "Internet Explorer_Server",
                                       "MicrosoftSilverlight",
                               });

複数の Silverlight コントロールがある場合、UIAutomation でそれらを区別する方法がわかりません。上記のクラス パスから "MicrosoftSilverlight" エントリを削除して、Explorer ページへの参照を取得できるようにします。次に使用します

AutomationElement.FindAll(TreeScope.Children, new PropertyCondition(AutomationElement.ClassNameProperty, "MicrosoftSilverlight"))

すべての SilverlightControls を見つけてから、それらをそれぞれ調べて、それらを区別できる要素を見つけます。

拡張メソッドは次のとおりです。

public static class AutomationExtensions
{
    public static AutomationElement FindDescendentByClassPath(this AutomationElement element, IEnumerable<string> classNames)
    {
        var conditionPath = CreateClassNameConditionPath(classNames);

        return element.FindDescendentByConditionPath(conditionPath);
    }

    public static AutomationElement FindDescendentByConditionPath(this AutomationElement element, IEnumerable<Condition> conditionPath)
    {
        if (!conditionPath.Any())
        {
            return element;
        }

        var result = conditionPath.Aggregate(
            element,
            (parentElement, nextCondition) => parentElement == null
                                                  ? null
                                                  : parentElement.FindChildByCondition(nextCondition));

        return result;
    }

    public static AutomationElement FindChildByCondition(this AutomationElement element, Condition condition)
    {
        var result = element.FindFirst(
            TreeScope.Children,
            condition);

        return result;
    }

    public static IEnumerable<Condition> CreateClassNameConditionPath(IEnumerable<string> classNames)
    {
        return classNames.Select(name => new PropertyCondition(AutomationElement.ClassNameProperty, name, PropertyConditionFlags.IgnoreCase)).ToArray();
    }
}
于 2010-08-05T09:51:33.757 に答える