C#使用System.Windows.Automation如何获取AutomationElement的FullDescription属性
C# System.Windows.Automation 访问FullDescription属性方案
在System.Windows.Automation库中,FullDescription属于UIA规范定义的附加属性,未直接作为AutomationElement的公开属性暴露,需通过属性查询接口访问。

实现要求
- 项目目标框架需为 .NET Framework 4.7.1及以上,或.NET Core 3.0/.NET 5及以上
- 已正确引用
UIAutomationClient程序集
代码实现
常规读取方式
// 假设已获取到目标控件的AutomationElement实例targetElement object descValue = targetElement.GetCurrentPropertyValue(AutomationElementIdentifiers.FullDescriptionProperty); string fullDescription = descValue as string; if (!string.IsNullOrEmpty(fullDescription)) { // 此处编写对应业务逻辑 }
无异常安全读取方式
if (targetElement.TryGetCurrentPropertyValue( AutomationElementIdentifiers.FullDescriptionProperty, ignoreDefaultValue: false, out object result)) { string fullDescription = result as string; // 此处编写对应业务逻辑 }
低版本框架兼容写法
如果框架版本过低找不到AutomationElementIdentifiers.FullDescriptionProperty,可使用该属性的固定UIA ID查询:
AutomationProperty fullDescProp = AutomationProperty.LookupById(30159); object descValue = targetElement.GetCurrentPropertyValue(fullDescProp); string fullDescription = descValue as string;
注意事项
- 部分老旧控件或未做无障碍适配的控件不会设置FullDescription属性,此时查询结果为空属于正常情况
- 若使用缓存模式获取属性,需提前将
FullDescriptionProperty加入到CacheRequest的属性列表中
内容的提问来源于stack exchange,提问作者Dmitriy Kryuchkov
相关产品推荐
相关产品推荐

