我有一个MainWindow
页面和一个LoadingWindow
。 MainWindow
的按钮会自行关闭并打开LoadingWindow
。我想要的是当用户关闭“加载”窗口并作为新实例回到MainWindow时。
MainWindow.xaml.cs
private void Button_Click(object sender, RoutedEventArgs e)
{
LoadingWindow load = new LoadingWindow(calibration , "MainWindow" , this);
Mouse.OverrideCursor = null;
Application.Current.MainWindow.Close();
load.ShowDialog(); // Exception is here the next time it is called
}
LoadingWindow.xaml.cs
private void Close_Button(object sender, RoutedEventArgs e)
{
MainWindow main = new MainWindow();
this.Close();
main.ShowDialog();
}
现在,当我尝试关闭“加载”窗口并按Button_Click时,load.ShowDialog()
会显示以下错误,尽管我正在声明它的新实例。
System.InvalidOperationException: Cannot set Visibility or call Show, ShowDialog, or WindowInteropHelper.EnsureHandle after a Window has closed
我读到您关闭窗口后无法打开窗口,但是我有一个新实例,该实例不会出现此问题。
答案 0 :(得分:1)
在主窗口上不要使用Application.Current.MainWindow
实例,而应使用this.Close()
。
private void Button_Click(object sender, RoutedEventArgs e)
{
LoadingWindow load = new LoadingWindow(...);
Mouse.OverrideCursor = null;
//Application.Current.MainWindow.Close();
this.Close();
load.ShowDialog(); // Exception is here the next time it is called
}