UI オートメーション要素のプロパティの取得

Note

このドキュメントは、System.Windows.Automation 名前空間で定義されているマネージド UI オートメーション クラスを使用する .NET Framework 開発者を対象としています。 UI オートメーションの最新情報については、Windows Automation API の「UI オートメーション」を参照してください。

このトピックでは、UI オートメーション要素のプロパティを取得する方法を示します。

現在のプロパティ値を取得する

  1. 取得するプロパティが含まれている AutomationElement を取得します。

  2. GetCurrentPropertyValue を呼び出すか、Current プロパティ構造体を取得し、そのメンバーの 1 つから値を取得します。

キャッシュされたプロパティ値を取得する

  1. 取得するプロパティが含まれている AutomationElement を取得します。 プロパティは、CacheRequest で指定されている必要があります。

  2. GetCachedPropertyValue を呼び出すか、Cached プロパティ構造体を取得し、そのメンバーの 1 つから値を取得します。

次の例は、AutomationElement の現在のプロパティを取得するさまざまな方法を示しています。

void PropertyCallsExample(AutomationElement elementList)
{
    // The following two calls are equivalent.
    string name = elementList.Current.Name;
    name = elementList.GetCurrentPropertyValue(AutomationElement.NameProperty) as string;

    // The following shows how to ignore the default property, which
    //  would probably be an empty string if the property is not supported.
    //  Passing "false" as the second parameter is equivalent to using the overload
    //  that does not have this parameter.
    object help = elementList.GetCurrentPropertyValue(AutomationElement.HelpTextProperty, true);
    if (help == AutomationElement.NotSupported)
    {
        help = "No help available";
    }
    string helpText = (string)help;
}
Sub PropertyCallsExample(ByVal elementList As AutomationElement)
    ' The following two calls are equivalent.
    Dim name As String = elementList.Current.Name
    name = CStr(elementList.GetCurrentPropertyValue(AutomationElement.NameProperty))

    ' The following shows how to ignore the default property, which 
    '  would probably be an empty string if the property is not supported.
    '  Passing "false" as the second parameter is equivalent to using the overload
    '  that does not have this parameter.
    Dim help As Object = elementList.GetCurrentPropertyValue(AutomationElement.HelpTextProperty, True)
    If help Is AutomationElement.NotSupported Then
        help = "No help available"
    End If
    Dim helpText As String = CStr(help)

End Sub

関連項目