简体   繁体   English

如何关闭隐藏的窗口(WPF应用程序)?

[英]How to close a hidden window (WPF application)?

In my application (WPF) i have this window: 在我的应用程序(WPF)中,我有以下窗口:

public partial class Window1 : Window and in the Xaml x:Class="WpfApplication1.Window1" public partial class Window1 : Window和Xaml中的x:Class="WpfApplication1.Window1"

Now, when i switch to and from the main to window 1 and back, i us the Visibility.Hidden and Visibility.Visible to hide them and make them show again to the user. 现在,当我从主窗口切换到窗口1并从窗口切换回来时,我们将使用Visibility.Hidden和Visibility.Visible隐藏它们并将它们再次显示给用户。

What i try to do now, is make a test button in the main window, that says: Close Window1. 我现在尝试执行的操作是在主窗口中创建一个测试按钮,内容为:关闭Window1。 This window is hidden, but i really want to close it in the background. 该窗口是隐藏的,但我真的想在后台将其关闭。 at first i though to just use the Window.Close(); 起初我虽然只是使用Window.Close(); but that does not seem to do the trick. 但这似乎并不能解决问题。

So, how should i do this in a correct way ? 那么,我应该如何正确地做到这一点? Thank you very much in advance. 提前非常感谢您。

EDIT 1 - making question more clear 编辑1-使问题更清晰

To open the window1 in my Main window, i use this part 要在主窗口中打开window1,请使用此部分

Window1 W1 = null; // Initialise Field.
  public void CalcTabel_Click(object sender, RoutedEventArgs e)
   {
    if (W1 == null)
    {
     W1 = new Window1();
     W1.Hoofdmenu = this;
     W1.checkLang();
     W1.Show();
    }
   else
    {
     W1.checkLang();
     W1.Visibility = Visibility.Visible;
    }
   this.Visibility = Visibility.Hidden;
   }

On window 1 there is a Back button, that has this snip-it of code in it (Where "Hoofdmenu" us the main window): 在窗口1上有一个“后退”按钮,其中包含以下代码片段(在主窗口中,“ Hoofdmenu”位于此处):

Hoofdmenu.updateStatistics();
Hoofdmenu.Visibility = Visibility.Visible;
this.Visibility = Visibility.Hidden;

But again, this time when standing in the main window (so window 1 is hidden) i want to close down that window 1. but using W1.Close() does not seem to work. 但是,这一次, 当我站在主窗口中时(因此窗口1被隐藏了),我想关闭该窗口1。但是使用W1.Close()似乎不起作用。 So i am looking for a way to Close that window 1, not change its visibility. 因此,我正在寻找一种关闭该窗口1的方法, 而不是更改其可见性。

EDIT 2 - Solution 编辑2-解决方案

So using W1.Close(); 因此使用W1.Close(); did not work, although a small change this.W1.Close(); 没有工作,虽然小的变化this.W1.Close(); did work in fact :) 确实工作了:)

You can create object of Form2 in window and intialize its visiblity to false. 您可以在窗口中创建Form2的对象,并将其可见性初始化为false。

On click of button you can simpliy say 点击按钮,您可以简单地说

public partial class MainWindow : Window
  {
    private Window1 window2;

    public MainWindow()
    {
      InitializeComponent();
      this.window2 = new Window1();

      this.window2.Show();
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
      this.window2.Visibility = System.Windows.Visibility.Hidden;
    }
  }

to make it visible again 使它再次可见

after reading your code, thats not possible the way you want it. 阅读您的代码后,那不是您想要的方式。 the window1 instance is local object. window1实例是本地对象。 so you cant reach it out side if this method. 因此,如果使用此方法,则无法将其伸出。 the best was is to have a close button on window1 with this.close() or you create a global instance in your main window and then check if not null then close it. 最好的办法是在window1上使用this.close()关闭按钮,或者在主窗口中创建一个全局实例,然后检查是否不为null,然后将其关闭。

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

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