如果有两个窗口,主要是A和B,如何使用在窗口B中编写的代码关闭窗口A。
主窗口
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();
}
}
}
回复您的评论,关闭另一个窗体创建的窗口很简单,只需调用已创建窗体的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();
}
}
}
}
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;
}
}
}
}
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;
}
}
}
我认为这比建议的解决方案更好,因为除了给窗口添加唯一的标签外,你不需要干扰你的Windows。 我只在小项目中使用过这个,没有风险导致事物不唯一,我不会忘记10-12个窗口!
另一个建议的解决方案有点傻(我没有50个声望来评论它),如果你已经有对象的引用,你可以直接调用win.close() ...
这很简单,只需创建一个公共类和方法,如下所示:
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 (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;
}