从WPF中的另一个窗口关闭窗口

7
如果有两个窗口,主要是A和B,如何使用在窗口B中编写的代码关闭窗口A。
4个回答

6
您最好的选择是在Window B上创建一个属性,将创建Window的窗口传递给该属性。类似于这样的操作。我有一个名为MainWindow的窗口和一个名为Window2的第二个窗口。

主窗口

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        Window2 secondForm;

        public MainWindow()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            secondForm = new Window2();
            secondForm.setCreatingForm =this;
            secondForm.Show();
        }
    }
}

Window2

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for Window2.xaml
    /// </summary>
    public partial class Window2 : Window
    {
        Window creatingForm;

        public Window2()
        {
            InitializeComponent();
        }

        public Window setCreatingForm
        {
            get { return creatingForm; }
            set { creatingForm = value; }
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            if (creatingForm != null)
                creatingForm.Close();

        }

    }
}

回复您的评论,关闭另一个窗体创建的窗口很简单,只需调用已创建窗体的Close方法:

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        Window2 secondForm;

        public MainWindow()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            if (secondForm == null)
            {
                secondForm = new Window2();
                secondForm.Show();
            }
            else
                secondForm.Activate();
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            if (secondForm != null)
            {
                secondForm.Close();
                secondForm = new Window2();
                //How ever you are passing information to the secondWindow
                secondForm.Show();
            }

        }
    }
}


干得好。实际上,我想从MainWindow关闭Window2。提前致谢。 - Anoop Mohan
@AnoopMohan 通过创建它的窗体关闭一个窗体要容易得多,你只需要保留对创建的窗体的引用并在其上调用Close方法即可。 - Mark Hall
谢谢您先生。现在我可以关闭窗口了。但是我还需要做一件事...我想在同一个事件中关闭窗口并打开具有不同值的相同窗口。抱歉打扰您了。提前致谢。 - Anoop Mohan
@AnoopMohan,我已将button2_click事件更改为重新打开窗口,但我不确定您传递给secondWindow的值是什么/如何传递,因此我无法更具体地说明。 - Mark Hall

4
这里有一种方法可以从任何其他窗口关闭任何窗口。您可以通过为您的窗口提供某些唯一标识符,然后在foreach循环中搜索该标识符来修改它以与多个实例一起工作。
public static class Helper
{
    public static void CloseWindowOfWhichThereIsOnlyOne<T>()
    {
        Assembly currentAssembly = Assembly.GetExecutingAssembly();
        foreach (Window w in Application.Current.Windows)
        {
            if (w.GetType().Assembly == currentAssembly && w is T)
            {
                w.Close();
                break;
            }
        }
    }
}

或者使用唯一标识符“fudge”:
    public static void CloseWIndowUsingIdentifier(string windowTag)
    {
        Assembly currentAssembly = Assembly.GetExecutingAssembly();
        foreach (Window w in Application.Current.Windows)
        {
            if (w.GetType().Assembly == currentAssembly && w.Tag.Equals(windowTag))
            {
                w.Close();
                break;
            }
        }
    }

我认为这比建议的解决方案更好,因为除了给窗口添加唯一的标签外,你不需要干扰你的Windows。 我只在小项目中使用过这个,没有风险导致事物不唯一,我不会忘记10-12个窗口!

另一个建议的解决方案有点傻(我没有50个声望来评论它),如果你已经有对象的引用,你可以直接调用win.close() ...


1

这很简单,只需创建一个公共类和方法,如下所示:

class Helper
{
 public static void CloseWindow(Window x)
    {
        Assembly currentAssembly = Assembly.GetExecutingAssembly();
      //  int count = Application.Current.Windows;
        foreach (Window w in Application.Current.Windows)
        {
            //Form f = Application.OpenForms[i];
            if (w.GetType().Assembly == currentAssembly && w==x)
            {
                w.Close();
            }
        }
    }
}

现在你可以从任何想要关闭窗口的地方调用这个函数,像这样。

 Helper.CloseWindow(win);//win is object of window which you want to close.

希望这可以帮到你。

0
        foreach (Window w in Application.Current.Windows)
        {
            if (w.Name != "Main_Window_wind" )
            {
                w.Visibility = System.Windows.Visibility.Hidden;
            }
        }
//name is the x:Name="Main_Window_wind" in xaml

现在您可以关闭所有窗口,而不关闭名为Main_Window_wind的窗口。如果要添加另一个窗口以避免关闭,请使用以下if语句:w.Name != "Main_Window_wind" && w.Name != "AnyOther_Window_wind" &&...

更快捷的方法如下:

        for (int intCounter = App.Current.Windows.Count - 1; intCounter > -1; intCounter--)
        {

            if (App.Current.Windows[intCounter].Name != "Main_Window_wind")
                App.Current.Windows[intCounter].Visibility = System.Windows.Visibility.Hidden;
        }

2
请在您的解决方案中添加一些注释,说明它为什么以及如何解决问题。 - Bhavesh Odedra

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接