在浏览器中托管 Word - AutomationElement IsWindowPatternAvailable - 如何设置?

Hosting Word in Browser - AutomationElement IsWindowPatternAvailable - how to set it?

我们在 WPF application 中的 WebBrowser control 中托管 MS-Word 文档。

WebBrowser control 在导航到选定的 MS-Word 文档期间显示以下对话框:

我们尝试使用 AutomationElement 以编程方式关闭对话。该代码在测试应用程序中没有任何问题。当我们在实际应用程序中调整代码时(edit 文件,使用 mail merge 显示文件),只有 mail merge 部分正确关闭对话。在另一种情况下,找不到对话的 AutomationElement。

我们发现当对话的 AutomationElement 有 IsWindowPatternAvailable = false 时我们的代码失败了。

有没有办法提前设置这个属性?或者为什么它在一种情况下为真而在另一种情况下为假的原因?

测试应用程序是一个 "standard WPF-Application" 项目。它只包含 MainWindow.xaml.cs 和 MainWindow.xaml。 单击按钮设置 SourceWebBrowser:

    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;

                }
            }
        }
    }

我们的实际应用稍微复杂一些,使用了MVVMPRISM 5WCF。我们使用 WCF 从服务器加载 Word 文档。文件保存在 %temp%。

两个 ViewModel(编辑文档/显示合并文档,每个都在一个 different module 中)发布一个 eventView 订阅:

    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.exeTreeView 中看不到对话,但我们可以通过单击对话来访问对话的属性。 在 inspect.exe 中,我们看到以下内容 ancestors

当我们使用代码编辑 "merge" 模块中的文档时,对话正确关闭。两个模块引用相同的 UIAutomation DLL(UIAutomationClient、UIAutomationProvider)。

这里提到了类似的问题:AutomationElement shows up using Inspect.exe but does show not up ... 使用 TreeWalker 或搜索 AutomationElement.RootElement 的完整 Subtree 无效。

欢迎提供 IsWindowPatternAvailable 行为方式的任何线索。也欢迎提供有关如何关闭“文件下载”对话框的其他建议。

终于在 blog 使用 UIAutomationEvents 的帮助下找到了解决我的问题的方法。

    AutomationEventHandler UIAEventHandler = new AutomationEventHandler(OnUIAEvent);

    Automation.AddAutomationEventHandler(WindowPattern.WindowOpenedEvent,
                    AutomationElement.RootElement,
                    TreeScope.Descendants, UIAEventHandler);
    private static void OnUIAEvent(object src, AutomationEventArgs e)
    {
        AutomationElement element = src as AutomationElement;

        if (element == null)
        {
            return;
        }

        AutomationElement openButton = element.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.AutomationIdProperty, "4426"));

        if (openButton != null)
        {
            openButton.SetFocus();

            ((InvokePattern)openButton.GetCurrentPattern(InvokePattern.Pattern)).Invoke();
        }
    }