7

如果两个窗口主要打开 A 和 B,如何使用编写在窗口 B 上的代码关闭窗口 A。

4

4 回答 4

5

您最好的选择是在 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();
        }
    }
}

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

        }
    }
}

于 2012-07-28T04:55:19.550 回答
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() ......

于 2013-08-28T19:09:17.320 回答
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.

希望这可以帮助。

于 2012-07-28T05:07:49.853 回答
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 的情况下关闭所有窗口,您可以在以下条件下添加另一个不关闭的窗口: 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;
        }
于 2015-05-11T11:00:25.117 回答