【问题标题】:Different behavior with FindAll for AutomationElement between MC++ and C#MC++ 和 C# 之间的 FindAll for AutomationElement 的不同行为
【发布时间】:2012-12-04 06:28:24
【问题描述】:

我正在尝试使用托管 C++ 中的 UI 自动化来查找 DataGrid 控件的 ControlType.DataItem 子项。以下 sn-p 在 C# 中对已知的 HWND 值起作用:

var automationElement = AutomationElement.FromHandle(new IntPtr(0x000602AE));
var propertyCondition = new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.DataItem);
var dataItems = automationElement.FindAll(TreeScope.Subtree, propertyCondition).Count;
Console.WriteLine("Found {0} DataItem(s)", dataItems);

这会产生以下输出:

Found 2 DataItem(s)

将代码转换为 MC++ 会产生零结果。这是转换后的 MC++ 代码:

auto automationElement = AutomationElement::FromHandle(IntPtr(0x000602AE));
auto propertyCondition = gcnew PropertyCondition(AutomationElement::ControlTypeProperty, ControlType::DataItem);
auto dataItems = automationElement->FindAll(TreeScope::Subtree, propertyCondition)->Count;
Console::WriteLine("Found {0} DataItem(s)", dataItems);

有没有其他人在使用托管 C++ 的 UI 自动化时遇到过这个问题?我过去曾将 MC++ 用于 UIA,这是我在 C# 中使用它时遇到的第一个区别。提前感谢您提供任何信息。

【问题讨论】:

    标签: c# ui-automation managed-c++ uia


    【解决方案1】:

    我不确定为什么会发生这种情况,但是每当我将 UI 自动化代码提取到另一个类中并像这样调用它时,它都会起作用。我并不声称自己是 MC++ 或它如何加载 .NET 框架方面的专家,但这就是我解决问题的方法。

    创建AutomatedTable

    AutomatedTable.h

    #pragma once
    using namespace System;
    using namespace System::Windows::Automation;
    
    ref class AutomatedTable {
    public:
        AutomatedTable(const HWND windowHandle);
    
        property int RowCount {
            int get();
        }
    
    private:
        AutomationElement^ _tableElement;
    };
    

    AutomatedTable.cpp

    #include "StdAfx.h"
    #include "AutomatedTable.h"
    
    AutomatedTable::AutomatedTable(const HWND windowHandle) {
        _tableElement = AutomationElement::FromHandle(IntPtr(windowHandle));
    }
    
    int AutomatedTable::RowCount::get() {
        auto dataItemProperty = gcnew PropertyCondition(AutomationElement::ControlTypeProperty, ControlType::DataItem);
        return _tableElement->FindAll(TreeScope::Subtree, dataItemProperty)->Count;
    }
    

    main.cpp调用

    main.cpp

    #include "stdafx.h"
    #include "AutomatedTable.h"
    
    int main(array<System::String ^> ^args) {
        auto automatedTable = gcnew AutomatedTable((HWND)0x000602AE);
        Console::WriteLine("Found {0} DataItem(s)", automatedTable->RowCount);
        return 0;
    }
    

    输出

    Found 2 DataItem(s)
    

    任何关于为什么这有任何不同的见解将不胜感激:-)

    【讨论】:

      猜你喜欢
      • 2014-04-25
      • 1970-01-01
      • 1970-01-01
      • 2020-11-13
      • 1970-01-01
      • 1970-01-01
      • 2014-11-01
      • 1970-01-01
      • 2018-05-07
      相关资源
      最近更新 更多