在 Wpf 中关闭另一个窗口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11698328/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 04:45:49  来源:igfitidea点击:

Close a Window from Another In Wpf

wpfc#-4.0

提问by Anoop Mohan

If two Window is opened mainly A and B, how to close Window A using code that written on Window B.

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

采纳答案by Mark Hall

Your best bet would be to create a property on Window B that you pass the creating Window to. Something like this. I have a Window named MainWindow and a second Window named Window2.

最好的办法是在 Window B 上创建一个属性,您将创建的 Window 传递给该属性。像这样的东西。我有一个名为 MainWindow 的窗口和一个名为 Window2 的第二个窗口。

Main Window

主窗口

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

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

        }

    }
}


In respose to your comment, closing a window that was created by another form is as easy as calling the Close Method of the created Form:

根据您的评论,关闭由另一个表单创建的窗口就像调用创建的表单的 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();
            }

        }
    }
}


回答by Ravendarksky

Here is a way to close any window from any other window. You can modify it to work with multiple instances by giving your windows some unique identifier and then just searching for that in the foreach loop.

这是一种从任何其他窗口关闭任何窗口的方法。您可以修改它以通过为您的窗口提供一些唯一标识符然后在 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;
            }
        }
    }
}

Or with a unique identifier "fudge":

或者使用唯一标识符“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;
            }
        }
    }

I like this better than the suggested solution because you don't need to mess with your windows, other than to give them unique tags. I've only been using this for small projects where there is no risk of things not being unique, I'm not going to lose track of 10-12 windows!

我比建议的解决方案更喜欢这个,因为你不需要弄乱你的窗户,除了给它们独特的标签。我只将它用于没有事物不独特的风险的小项目,我不会忘记 10-12 个窗口!

The other suggested solution is a little silly (I don't have 50 karma to comment on it) as you could just call win.close() if you already had a reference to the object...

另一个建议的解决方案有点愚蠢(我没有 50 因果报应对其进行评论),因为如果您已经拥有对该对象的引用,则可以调用 win.close() ...

回答by Manish Parakhiya

it is very simple make one public class and method like this

制作一个这样的公共类和方法非常简单

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

now call this function from where you want close window like this .

现在从你想要关闭窗口的地方调用这个函数。

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

hope this helps.

希望这可以帮助。

回答by Christos

        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

You can close now as hiden all windows without closing the named Main_Window_wind , you can add another window to be not closed with this in if: w.Name != "Main_Window_wind" && w.Name != "AnyOther_Window_wind" &&...

您现在可以关闭所有窗口而不关闭命名的 Main_Window_wind ,您可以添加另一个不关闭的窗口,如果: w.Name != "Main_Window_wind" && w.Name != "AnyOther_Window_wind" &&...

a quicker way is this:

一个更快的方法是这样的:

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