WPF C#关闭窗口onclick

时间:2016-02-28 11:23:55

标签: c# wpf xaml window mui

我有一个使用FirstFloor MUI Framework应用程序的WPF C#,在启动时检查设置并将显示特定的启动uri,如下所示;

if(somethings_true) {

Application curApp = Application.Current;
//ModernWindow
curApp.StartupUri = new Uri("MainWindow.xaml", UriKind.RelativeOrAbsolute);

}else{

Application curApp = Application.Current;
//ModernWindow
curApp.StartupUri = new Uri("OtherWindow.xaml", UriKind.RelativeOrAbsolute);

}

工作正常,但是当“OtherWindow.xaml”处于活动状态时,它首先会有一个onclick事件进行其他检查,并在完成后打开MainWindow.xaml。但是在打开MainWindow.xaml的Button_Click()中,我无法关闭OtherWindow.xaml并且我在OtherWindow.xaml中尝试过..

this.Close();

&安培;

var OtherWin = new OtherWindow();
OtherWin.Close();

&安培;

var w = Application.Current.Windows[0];
w.Hide();
//Only hides the OtherWindows.xaml (Still runs hidden in background even after MainWindow.xaml is closed)

我使用下面的代码来检查是否仍然在MainWindow.xaml中打开了OtherWindow.xaml,在其中它声明了它;

foreach (var wndOtherWindow in Application.Current.Windows)
{
if (wndOtherWindow is OtherWindow)
{
//Its Open Still...
//How to close() "OtherWindow.xaml" from here?

}
}

还有另一种关闭()OtherWindow.xaml的方法吗?

1 个答案:

答案 0 :(得分:2)

你应该转换为Window或特定类型(在你的情况下是OtherWindow),这样你就可以调用Close()方法。试试这个:

    foreach (var wndOtherWindow in Application.Current.Windows)
    {
        if (wndOtherWindow is OtherWindow)
        {
           (wndOtherWindow as Window).Close();
        }
    }
希望有所帮助。