2

MS-WordでドキュメントをホストWebBrowser controlしていWPF applicationます。

WebBrowser control、選択した MS-Word ドキュメントへのナビゲーション中に次のダイアログを表示します。

ファイルのダウンロード ダイアログ

を使用してプログラムでダイアログを閉じようとしAutomationElementます。コードは、テスト アプリケーションで問題なく動作します。コードを実際のアプリケーション (editファイル、show file with 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;

                }
            }
        }
    }

実際のアプリケーションはもう少し複雑で、 と を使用MVVMPRISM 5ますWCF。WCF を使用して、サーバーから Word ドキュメントを読み込みます。ファイルは %temp% に保存されます。

2 つViewModelの s (ドキュメントの編集 / マージされたドキュメントの表示、それぞれ a 内)は、s がサブスクライブするを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;
        }

    }

IsWindowPatternAvailableの助けを借りての違いを発見しましたinspect.exeIsWindowPatternAvailable = trueダイアログが and の直接の子である場合、Desktop見つけることができます。IsWindowPatternAvailable = falseにダイアログが表示されない場合でも、ダイアログをクリックしてダイアログのプロパティにアクセスできますTreeViewinspect.exeではinspect.exe、次のことがわかりますancestors

  • 対話そのもの
  • 要素クラス名: シェルの埋め込み
  • ウェブブラウザ
  • ViewEdit (ドキュメントを編集するためのビュー)
  • 応用

コードを使用して「マージ」モジュールでドキュメントを編集すると、ダイアログが正しく閉じられます。両方のモジュールが同じUIAutomationDLL (UIAutomationClient、UIAutomationProvider) を参照します。

同様の問題がここで言及されています: AutomationElement は Inspect.exe を使用して表示されますが、表示されません ... を使用しTreeWalkerたり、全体を検索したりすることSubtreeAutomationElement.RootElementできません。

IsWindowPatternAvailableそのように振る舞う理由の手がかりは大歓迎です。ファイルのダウンロード ダイアログを閉じる方法に関するその他の提案も歓迎します。

4

1 に答える 1