我正在使用MS UIA框架及其内置的自动化事件处理。我在WindowClosedEvent上看到了意外的行为。
也许我在这里做错了,但我的理解是Console调用应该执行一次。当我运行上面的代码时,Console行执行两次。
当我的TestApp关闭时,它没有运行的特殊事件。测试应用程序也只是一个WPF窗口。
[TestMethod]
public void TestMethod1()
{
Process.Start(@"TestApp.exe");
Thread.Sleep(2000); //sleep to wait for proc to boot with ui
var windowElement = AutomationElement.RootElement.FindFirst(TreeScope.Descendants,
new AndCondition(new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Window),
new PropertyCondition(AutomationElement.NameProperty, "TestApp"), new PropertyCondition(AutomationElement.AutomationIdProperty, "TestAppAutomationId")));
Automation.AddAutomationEventHandler(WindowPattern.WindowClosedEvent, windowElement, TreeScope.Element,
OnClose);
var windowPattern = (WindowPattern)windowElement.GetCurrentPattern(WindowPattern.Pattern);
windowPattern.Close();
Thread.Sleep(2000); //sleep to wait for uia's event threads to fire and finish.
}
public void OnClose(object sender, AutomationEventArgs e)
{
Console.WriteLine("test"); //this is run twice during the test
}
我在Windows 10上运行.NET 4.5.2。
如果我在这里做错了会导致处理程序多次触发,请告诉我。
感谢。
答案 0 :(得分:0)
解决方案问题是我们发现在返回事件调用时它会返回一个值
( AutomationPropertyChangedEventArgs e)
上面的事件具有传递给函数的属性值
private void Property_Change_Event(object src, AutomationPropertyChangedEventArgs e)
{
AutomationElement sourceElement = src as AutomationElement;
if ( e.Property == WindowPatternIdentifiers.WindowClosedEvent)
{
//manage e.NewValue == 0 or 1
}