获取使用自动化事件C#关闭的窗口的名称 [英] Get name of window which is being closed using automation events c#

查看:303
本文介绍了获取使用自动化事件C#关闭的窗口的名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在一个项目中,我需要获取正在关闭的窗口的名称. 我为此使用 C#自动化事件.

I'm working on a project where I need to get the name of windows which is being closed. I'm using C# Automation events for this.

我粘贴了下面使用的代码:

I've pasted below the code that I'm using:

Automation.AddAutomationEventHandler(WindowPattern.WindowClosedEvent,
AutomationElement.RootElement, TreeScope.Subtree, (sender, eve) =>
{
    AutomationElement winElemnt = sender as AutomationElement;
    if (winElemnt != null)
    {
        Console.WriteLine("Window closed : " + winElemnt.Current.Name);
    }
 });

以上代码在任何窗口关闭时触发,但执行时出现以下错误:

The above code get triggered when any window closes but I'm getting following error on execution:

  1. 变量 sender 的值将为 null .
  2. 无法获取自动化元素的 Current.Name (大多数情况下,我会收到此错误)
  1. Value of variable sender will be null.
  2. Unable to get automation element's Current.Name (I'm getting this error most of the time)

在调试中,我能够发现第二个错误是由于在完成事件处理程序执行之前关闭了窗口.

请让我知道如何解决这些错误并获取触发关闭的窗口的名称.

Please let me know how to fix these errors and get the name of window for which close is triggered.

预先感谢

推荐答案

我相信您需要使用缓存,以便在窗口关闭时能够检索该属性,因为自动化元素将消失.

I believe you need to use caching to be able to retrieve the property when the window closes since the automation element will be gone.

WindowPattern的注释如下:

The remarks for the WindowPattern says the following

客户端应用程序可能需要侦听来自 缓存的对象,因为从UI Automation控件中删除了一个窗口 关闭后立即查看结构.

A client application may need to listen for WindowClosedEvent from a cached object since a window is removed from the UI Automation control view structure immediately upon being closed.

这篇关于获取使用自动化事件C#关闭的窗口的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆