0

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?

Machavity
  • 30,841
  • 27
  • 92
  • 100
BENN1TH
  • 2,003
  • 2
  • 31
  • 42

1 Answers1

3

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.

Jamaxack
  • 2,400
  • 2
  • 24
  • 42