简体   繁体   中英

WPF C# Close Window onclick

I have a WPF C# using FirstFloor MUI Framework app, that on start, checks for settings and will show the specific startup uri as per below;

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);

}

which works fine, however when the "OtherWindow.xaml" is active first it has a onclick event that does other checks, and on finishing opens the MainWindow.xaml. But in the Button_Click() which does the opening of MainWindow.xaml, I cant get the OtherWindow.xaml to close and ive tried inside 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)

I use the below code to check if OtherWindow.xaml is still open inside MainWindow.xaml in which it states that it does;

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

}
}

Is there another way to close() the OtherWindow.xaml?

You should cast to Window or specific type(in your case OtherWindow), that way you can call Close() method. Try this:

    foreach (var wndOtherWindow in Application.Current.Windows)
    {
        if (wndOtherWindow is OtherWindow)
        {
           (wndOtherWindow as Window).Close();
        }
    }

Hope helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM