[英]How to close window programmatically after calling window.showdialog?
我正在創建一個測試方法,其中我使用window.showdialog
方法打開一個窗口,但是直到單擊關閉按鈕,才開始執行其他代碼。
這是我的測試方法:
[TestMethod]
public void TestProductView()
{
SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());
ProductTreeDragDropViewModel viewmodel = new ProductTreeDragDropViewModel();
System.Windows.Window window = new System.Windows.Window();
ProductTreeDragDrop screen = new ProductTreeDragDrop();
screen.ViewModel = viewmodel;
window.Content = screen;
window.ShowDialog();
DoEvents();
Thread.Sleep(9000);
}
public void DoEvents()
{
DispatcherFrame frame = new DispatcherFrame();
Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Background, new DispatcherOperationCallback(ExitFrame), frame);
Dispatcher.PushFrame(frame);
}
注意:如果我使用window.show方法,則不會在viewmodels的數據源中返回數據。
現在,如何在執行window.showdialog
方法后以編程方式關閉窗口?
使用ShowDialog啟動對話框時,控件將轉到新打開的對話框。 因此,您需要在啟動的對話框中執行關閉操作。
或者,您可以嘗試
window.Show();
Thread.Sleep(10000);
window.Close();
DoEvents();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.