【问题标题】:Close a Window from Another In Wpf在 Wpf 中关闭另一个窗口
【发布时间】:2012-07-28 04:38:44
【问题描述】:

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

【问题讨论】:

    标签: wpf c#-4.0


    【解决方案1】:

    最好的办法是在 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();
    
            }
    
        }
    }
    

    根据您的评论,关闭由另一个表单创建的窗口就像调用已创建表单的关闭方法一样简单:

    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。提前致谢。
    • @AnoopMohan 通过创建表单的表单关闭表单要容易得多,您只需保留对创建表单的引用并在其上调用 Close 方法。
    • :- 谢谢先生。 .现在我可以关闭窗口了。但我还需要做一件事。我想在同一事件中关闭窗口并打开具有不同值的同一窗口。对不起,打扰您。 .提前致谢。 .
    • @AnoopMohan 我更改了 button2_click 事件以重新打开窗口,我不确定您传递给 secondWindow 的值是什么/如何,所以我无法比这更具体。
    【解决方案2】:

    这是一种从任何其他窗口关闭任何窗口的方法。您可以修改它以使用多个实例,方法是为您的窗口提供一些唯一标识符,然后在 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()...

    【讨论】:

      【解决方案3】:

      像这样创建一个公共类和方法非常简单

      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.
      

      希望这会有所帮助。

      【讨论】:

        【解决方案4】:
                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;
                }
        

        【讨论】:

        • 请添加一些关于您的解决方案的 cmet,说明它为什么以及如何解决问题
        猜你喜欢
        • 1970-01-01
        • 2010-11-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-12-12
        • 2022-01-07
        • 1970-01-01
        相关资源
        最近更新 更多