在Wpf中关闭另一个窗口

时间:2012-07-28 04:38:44

标签: wpf c#-4.0

如果主要打开两个Window A和B,如何使用写在Window B上的代码关闭Window A.

4 个答案:

答案 0 :(得分:5)

您最好的选择是在Window B上创建一个将创建窗口传递给的属性。像这样的东西。我有一个名为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();
        }
    }
}

<强>窗口2

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

        }

    }
}

在处理您的评论时,关闭由另一个表单创建的窗口就像调用创建的表单的关闭方法一样简单:

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

        }
    }
}

答案 1 :(得分: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;
            }
        }
    }

我比建议的解决方案更喜欢这个,因为你不需要弄乱你的窗户,除了给它们提供独特的标签。我只是在小项目中使用它,在那些没有独特的风险的情况下,我不会忘记10-12个窗口!

另一个建议的解决方案有点愚蠢(我没有50个业力来评论它),因为如果你已经有了对象的引用,你可以调用win.close()...

答案 2 :(得分: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.

希望这会有所帮助。

答案 3 :(得分: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”&amp;&amp;中添加另一个不关闭的窗口。 w.Name!=“AnyOther_Window_wind”&amp;&amp; ...

更快的方法是:

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