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;
}
}
}
}
実際のアプリケーションはもう少し複雑で、 と を使用MVVM
しPRISM 5
ますWCF
。WCF を使用して、サーバーから Word ドキュメントを読み込みます。ファイルは %temp% に保存されます。
2 つViewModel
の s (ドキュメントの編集 / マージされたドキュメントの表示、それぞれ a 内)は、s がサブスクライブするをdifferent module
発行します。event
View
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.exe
。IsWindowPatternAvailable = true
ダイアログが and の直接の子である場合、Desktop
見つけることができます。IsWindowPatternAvailable = false
にダイアログが表示されない場合でも、ダイアログをクリックしてダイアログのプロパティにアクセスできますTreeView
。inspect.exe
ではinspect.exe
、次のことがわかりますancestors
。
- 対話そのもの
- 要素クラス名: シェルの埋め込み
- ウェブブラウザ
- ViewEdit (ドキュメントを編集するためのビュー)
- 応用
コードを使用して「マージ」モジュールでドキュメントを編集すると、ダイアログが正しく閉じられます。両方のモジュールが同じUIAutomation
DLL (UIAutomationClient、UIAutomationProvider) を参照します。
同様の問題がここで言及されています: AutomationElement は Inspect.exe を使用して表示されますが、表示されません ...
を使用しTreeWalker
たり、全体を検索したりすることSubtree
はAutomationElement.RootElement
できません。
IsWindowPatternAvailable
そのように振る舞う理由の手がかりは大歓迎です。ファイルのダウンロード ダイアログを閉じる方法に関するその他の提案も歓迎します。