无法确定原因我使用uiacomwrapper而不是system.windows.automation使用相同的代码获得异常?

时间:2017-11-16 23:26:12

标签: c# com ui-automation com-interop microsoft-ui-automation

我正在使用Windows自动化进行一些实验。由于Windows 7和Windows 7中的更新模式,建议不再使用System.Windows.Automation托管代码。 8并使用新的COM库。我在NuGet上找到了一个名为UIAComWrapper的包装器。我在下面有一段代码调用窗口上的登录按钮。我也试过用按钮调用其他窗口。使用System.Windows.Automation库时,代码工作正常但在使用UIAComWrapper时,我在成功调用按钮2-3秒后得到异常。这是代码:

    static void Main(string[] args)
    {
        Automation.AddAutomationEventHandler(WindowPattern.WindowOpenedEvent, AutomationElement.RootElement, TreeScope.Subtree, (sender, e) =>
        {

            AutomationElement src = sender as AutomationElement;
            if (src != null && src.Current.Name == "Login Tester")
            {
                Console.WriteLine("Class : " + src.Current.ClassName);
                Console.WriteLine("Title : " + src.Current.Name);
                Console.WriteLine("Handle : " + src.Current.NativeWindowHandle);
                Console.WriteLine("ProcessId : " + src.Current.ProcessId);
                Console.WriteLine("ControlType : " + src.Current.ControlType);
                Console.WriteLine("AutomationId : " + src.Current.AutomationId);


                Condition conditions = new AndCondition(
                    new PropertyCondition(AutomationElement.IsEnabledProperty, true),
                    new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Button)
                    );

                AutomationElementCollection elementCollection = src.FindAll(TreeScope.Children, conditions);

                foreach (AutomationElement ae in elementCollection)
                {
                    if (ae.Current.AutomationId == "btnLogin")
                    {
                        InvokePattern ip = ae.GetCurrentPattern(InvokePattern.Pattern) as InvokePattern;
                            ip.Invoke();

                    }
                }
            }
        });

        Console.ReadLine();
    }
}   

以下是例外:

  Error HRESULT E_FAIL has been returned from a call to a COM component.

这是堆栈跟踪:

at UIAutomationClient.IUIAutomationInvokePattern.Invoke() at System.Windows.Automation.InvokePattern.Invoke() in d:\dev\UIAComWrapper\UiaComWrapper\InvokePattern.cs:line 37 at uiacomwrapperexperimentation2.Program.<>c.<Main>b__0_0(Object sender, AutomationEventArgs e) in C:\Users\****\Documents\Visual Studio 2017\Projects\uiacomwrapperexperimentation2\uiacomwrapperexperimentation2\Program.cs:line 40 at UIAComWrapperInternal.BasicEventListener.UIAutomationClient.IUIAutomationEventHandler.HandleAutomationEvent(IUIAutomationElement sender, Int32 eventId) in d:\dev\UIAComWrapper\UiaComWrapper\ClientEventList.cs:line 111

我已经阅读了一些帖子,说这个错误可能是由于项目缓存,并尝试了这些解决方案无济于事。这是包装器https://github.com/TestStack/UIAComWrapper

的Git

0 个答案:

没有答案