【发布时间】:2021-09-20 05:23:25
【问题描述】:
我正在开发一个 .NET 核心工作者服务 应用程序,该应用程序将监控少数 winforms 应用程序。 这些 winforms 应用程序很少需要任何人为干预,它们唯一需要的时候是显示一些子窗口对话框时,如下所示。目标是使用此 .NET 核心工作人员服务消除这些罕见的人机交互。
| App | Main App Window |
|---|---|
| someApp.exe |
| Child Window | Child Window Example Image | Action that I need to take |
|---|---|---|
| Information Window | Action 1: I just need to click that Ok button and get on with my life. I'll be doing this programmatically. | |
| Error Window | Action 2: I need to restart someApp.exe. Again, I'll be doing this programmatically. |
现在我可以像这样抓取主窗口和随后的子窗口:
var process = Process.GetProcesses().FirstOrDefault(p => p.ProcessName == "someApp");
// Get the main window of the process we're interested in:
AutomationElement appMainWindow = AutomationElement.FromHandle(process.MainWindowHandle);
// Now the child windows:
var childElements = appMainWindow.FindAll(TreeScope.Children, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Window));
if (childElements.Count > 0)
{
foreach (AutomationElement childElement in childElements)
{
//How do I check if this childElement is an information window or an error window so that I can take my appropriate action?
}
}
到目前为止我做了什么:
var controlType = childElement.Current.ControlType.LocalizedControlType;
这没有帮助,因为它对这两种情况都显示了 window。
所以我的问题是:
如何检查此childElement 是信息MessageBox 还是错误MessageBox,以便采取适当的措施?
谢谢!
【问题讨论】:
-
这两种 MessageBox 类型没有区别。唯一的问题是,在创建它们时,会请求不同的系统图标,并且该信息在创建 MessageBox 的代码之外不可用。它们都是完全相同类型的 MessageBox。
-
@KenWhite 感谢您的回复。在那种情况下,您对我如何区分两个 MessageBox 有什么建议吗?阅读错误/消息文本是唯一的选择吗?
-
我什至不确定你能做到这一点——这完全取决于 Windows 如何创建对话框本身。我的建议是联系编写 someApp.exe 的人,让他们为您提供关闭弃用消息的选项,并让他们修复导致运行时错误的任何问题,需要您重新启动它。
标签: c# .net-core ui-automation ui-testing microsoft-ui-automation