简体   繁体   English

WPF C# 关闭窗口 onclick

[英]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;我有一个使用 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);

}

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.这工作正常,但是当“OtherWindow.xaml”首先处于活动状态时,它有一个执行其他检查的 onclick 事件,并在完成时打开 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..但是在打开 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)

I use the below code to check if OtherWindow.xaml is still open inside MainWindow.xaml in which it states that it does;我使用下面的代码来检查 OtherWindow.xaml 是否仍然在 MainWindow.xaml 中打开,其中声明它确实如此;

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?有没有另一种方法来关闭()OtherWindow.xaml?

You should cast to Window or specific type(in your case OtherWindow), that way you can call Close() method.您应该转换为 Window 或特定类型(在您的情况下为 OtherWindow),这样您就可以调用 Close() 方法。 Try this:尝试这个:

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

Hope helps.希望有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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