【发布时间】:2016-08-09 14:01:48
【问题描述】:
我们在WebBrowser control 中托管MS-Word 文档在WPF application 中。
WebBrowser control 在导航到选定的 MS-Word 文档期间显示以下对话框:
我们尝试使用AutomationElement 以编程方式关闭对话。该代码在测试应用程序中没有任何问题。当我们在实际应用程序中调整代码时(edit 文件,用mail merge 显示文件),只有mail merge 部分正确关闭了对话框。在另一种情况下,无法找到对话的 AutomationElement。
我们发现,当对话的 AutomationElement 具有 IsWindowPatternAvailable = false 时,我们的代码会失败。
有没有办法提前设置这个属性?或者为什么它在一种情况下为真而在另一种情况下为假的原因?
测试应用程序是一个“标准 WPF 应用程序”项目。它只包含 MainWindow.xaml.cs 和 MainWindow.xaml。
单击按钮将Source 设置为WebBrowser:
private void Button_Click(object sender, RoutedEventArgs e)
{
Thread thread = new Thread(new ThreadStart(backgroundCheck));
thread.Start();
this.TestBrowser.Source = new Uri(@"path-to-document.doc");
thread.Abort();
}
backgroundCheck 搜索特定对话并调用Open 按钮
private void backgroundCheck()
{
Thread.Sleep(500);
while (true)
{
AutomationElement window = AutomationElement.RootElement.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Window));
if (window!= null)
{
AutomationElement downloadWindow = window.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Window));
if (downloadWindow != null)
{
AutomationElement button = downloadWindow.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.AutomationIdProperty, "4426"));
button.SetFocus();
(InvokePattern)button.GetCurrentPattern(InvokePattern.Pattern)).Invoke();
return;
}
}
}
}
我们的实际应用程序有点复杂,使用MVVM、PRISM 5 和WCF。我们使用 WCF 从服务器加载 Word 文档。文件保存在 %temp% 中。
两个ViewModels(编辑文档/显示合并文档,每个都在一个different module)发布一个event,Views 订阅:
public class VmExample
{
public delegate void BrowserNavigationEventHandler(string pfad);
public event BrowserNavigationEventHandler browserNavigate;
private void navigateToDocument()
{
browserNavigate("Path-To-Document.doc");
}
}
public partial class ViewMerge : UserControl
{
private VmExample _vm;
public ViewMerge()
{
InitializeComponent();
this.DataContextChanged += ViewMerge_DataContextChanged;
}
private void ViewMerge_DataContextChanged(object sender, System.Windows.DependencyPropertyChangedEventArgs e)
{
this._vm = e.NewValue as VmExample;
this._vm.browserNavigate += ViewMerge_browserNavigate;
this.DataContextChanged -= ViewMerge_DataContextChanged;
}
private void ViewMerge_browserNavigate(string path)
{
Thread threadCheckDownoadWindow = new Thread(backgroundCheck);
threadCheckDownoadWindow.Start();
this.wbBrowser.Source = new Uri(path);
threadCheckDownoadWindow.Abort();
this._vm.browserNavigate -= ViewMerge_browserNavigate;
}
}
在inspect.exe 的帮助下,我们发现了IsWindowPatternAvailable 的不同之处。当IsWindowPatternAvailable = true 对话是Desktop 的直接子代并且可以找到。当IsWindowPatternAvailable = false 时,我们在inspect.exe 的TreeView 中看不到对话,但我们可以通过单击它来访问对话的属性。
在inspect.exe 中,我们看到以下ancestors:
- 对话本身
- 元素类名:外壳嵌入
- 网络浏览器
- ViewEdit(查看以编辑文档)
- 应用
当我们使用代码在“合并”模块中编辑文档时,对话框正确关闭。两个模块都引用了相同的UIAutomation DLL(UIAutomationClient、UIAutomationProvider)。
这里提到了一个类似的问题:AutomationElement shows up using Inspect.exe but does show not up ...
使用TreeWalker 或搜索AutomationElement.RootElement 的完整Subtree 不起作用。
欢迎任何关于IsWindowPatternAvailable 行为方式的线索。也欢迎有关如何关闭文件下载对话框的其他建议。
【问题讨论】:
标签: c# wpf ui-automation automationelement