Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Wpf中从另一个窗口关闭窗口_Wpf_C# 4.0 - Fatal编程技术网

在Wpf中从另一个窗口关闭窗口

在Wpf中从另一个窗口关闭窗口,wpf,c#-4.0,Wpf,C# 4.0,如果两个窗口主要打开A和B,那么如何使用在窗口B上编写的代码关闭窗口A。您最好在窗口B上创建一个属性,并将创建窗口传递给该属性。像这样的。我有一个名为MainWindow的窗口和第二个名为Window2的窗口 主窗口 namespace WpfApplication1 { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial

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

您最好在窗口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();
        }
    }
}
命名空间WpfApplication1
{
/// 
///MainWindow.xaml的交互逻辑
/// 
公共部分类主窗口:窗口
{
窗口2第二形式;
公共主窗口()
{
初始化组件();
}
私有无效按钮1\u单击(对象发送者,路由目标)
{
secondForm=newwindow2();
secondForm.setCreatingForm=此;
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();

        }

    }
}
命名空间WpfApplication1
{
/// 
///Window2.xaml的交互逻辑
/// 
公共部分类Window2:Window
{
窗口创建窗体;
公共窗口2()
{
初始化组件();
}
公共窗口设置创建窗体
{
获取{return creatingForm;}
设置{creatingForm=value;}
}
私有无效按钮1\u单击(对象发送者,路由目标)
{
if(creatingForm!=null)
创建form.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();
            }

        }
    }
}
命名空间WpfApplication1
{
/// 
///MainWindow.xaml的交互逻辑
/// 
公共部分类主窗口:窗口
{
窗口2第二形式;
公共主窗口()
{
初始化组件();
}
私有无效按钮1\u单击(对象发送者,路由目标)
{
if(secondForm==null)
{
secondForm=newwindow2();
secondForm.Show();
}
其他的
secondForm.Activate();
}
私有无效按钮2\u单击(对象发送者,路由目标)
{
if(secondForm!=null)
{
secondForm.Close();
secondForm=newwindow2();
//您向第二个窗口传递信息的方式
secondForm.Show();
}
}
}
}


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

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.

希望这有帮助。

这里有一种方法可以从任何其他窗口关闭任何窗口。您可以修改它以处理多个实例,方法是为您的窗口提供一些唯一标识符,然后在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;
            }
        }
    }
}
与建议的解决方案相比,我更喜欢这个解决方案,因为您不需要弄乱您的窗口,只需要为它们提供独特的标签。我只在一些小项目中使用过它,在这些项目中不会出现不独特的情况,我不会忘记10-12个窗口

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

        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
您现在可以在隐藏所有窗口时关闭,而无需关闭命名的主窗口。如果:w.Name!=“主窗风”和w.名称!=“任何其他风窗”&&

更快的方法是:

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

-干得好。实际上我想从主窗口关闭Window2。提前谢谢。@AnoopMohan使用创建表单的表单关闭表单要容易得多,您只需保留对已创建表单的引用并调用其上的Close方法即可。:-谢谢您,先生。现在我可以关上窗户了。但我还需要做一件事。。我想在同一事件中关闭窗口并以不同的值打开同一窗口。对不起打扰你了。提前谢谢@AnoopMohan我更改了button2_click事件以重新打开窗口,我不确定您要传递给第二个窗口的值是什么/如何传递,因此我无法获得比这更具体的信息。请添加一些关于您的解决方案的注释,说明为什么以及如何解决问题
        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;
        }