【问题标题】:Windows UI AutomationWindows UI 自动化
【发布时间】:2011-03-10 22:18:14
【问题描述】:

我正在测试下面的示例代码,不知何故,每当我尝试运行它时,都会出现如下所示的错误。但是,calc.exe 进程已成功执行,那么句柄怎么可能为 null 或为零呢?我希望你能理解我想要表达的意思。谢谢!代码示例来自http://www.mathpirate.net/log/tag/system-windows-automation/

未处理的类型异常 'System.ArgumentException' 发生在 UIAutomationClient.dll 附加信息:hwnd 不能为 IntPtr.Zero 或 null。

//Launches the Windows Calculator and gets the Main Window's Handle.
Process calculatorProcess = Process.Start("calc.exe");
calculatorProcess.WaitForInputIdle();
IntPtr calculatorWindowHandle = calculatorProcess.MainWindowHandle;

//Here I use a window handle to get an AutomationElement for a specific window.
AutomationElement calculatorElement = AutomationElement.FromHandle(calculatorWindowHandle);

if(calculatorElement == null)
{
    throw new Exception("Uh-oh, couldn't find the calculator...");
}

//Walks some of the more interesting properties on the AutomationElement.
Console.WriteLine("--------Element");
Console.WriteLine("AutomationId: {0}", calculatorElement.Current.AutomationId);
Console.WriteLine("Name: {0}", calculatorElement.Current.Name);
Console.WriteLine("ClassName: {0}", calculatorElement.Current.ClassName);
Console.WriteLine("ControlType: {0}", calculatorElement.Current.ControlType.ProgrammaticName);
Console.WriteLine("IsEnabled: {0}", calculatorElement.Current.IsEnabled);
Console.WriteLine("IsOffscreen: {0}", calculatorElement.Current.IsOffscreen);
Console.WriteLine("ProcessId: {0}", calculatorElement.Current.ProcessId);

//Commented out because it requires another library reference. However, it's useful to see that this exists.
//Console.WriteLine("BoundingRectangle: {0}", calculatorElement.Current.BoundingRectangle);

Console.WriteLine("Supported Patterns:");
foreach (AutomationPattern supportedPattern in calculatorElement.GetSupportedPatterns())
{
    Console.WriteLine("\t{0}", supportedPattern.ProgrammaticName);
}

【问题讨论】:

    标签: c# user-interface automation


    【解决方案1】:

    您误解了WaitForInputIdle(对于该功能当前的功能来说,这是一个非常糟糕的名字)。您甚至在创建主窗口之前就要求提供主窗口的地址。结果,您最终将无效的窗口句柄传递给您的其他函数。

    编辑:如果您打算认真使用它,我强烈建议您使用 UI 自动化库,例如 white

    【讨论】:

    • 如果您使用白色,请注意它有一些问题;我发现这些在我当前的项目中非常重要。您可以在以下位置查看白色问题列表:white.codeplex.com/workitem/list/basic
    • 顺便说一句,这是推荐的使用 UI 自动化进行监控的想法吗?我有一个第 3 方应用程序,其中我想要监控的值并基于这些值,我想执行一些操作,即 UI 操作、单击按钮、更改文本字段值等。至于监控值,我是否必须单独监控每个控件?谢谢!
    猜你喜欢
    • 2012-02-29
    • 1970-01-01
    • 1970-01-01
    • 2013-03-25
    • 1970-01-01
    • 2011-01-12
    • 2013-12-03
    • 2014-01-25
    • 2018-11-17
    相关资源
    最近更新 更多