如果两个窗口打开主要是A和B,如何使用写在窗口B码关闭窗口一从另外一个关闭一个窗口在WPF
5
A
回答
5
你最好的选择是创建在窗口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();
}
}
}
窗口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();
}
}
}
在respose您的意见,关闭,是由另一种形式创建的窗口是调用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();
}
}
}
}
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.
希望这有助于。
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;
}
}
}
}
或者具有唯一标识符“忽悠”:
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()如果你已经有对象的引用......
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;
}
+2
请在解决问题的原因和方式上添加一些关于解决方案的意见 – 2015-05-11 12:23:43
相关问题
- 1. 在WPF MVVM中关闭一个窗口
- 2. 在另一个wpf窗口内部有一个wpf窗口
- 3. Java用JDialog关闭另一个窗口
- 4. 用javascript关闭另一个窗口
- 5. 一旦另一个窗口关闭,更改浏览器窗口
- 6. Qt在第一个关闭时打开另一个窗口
- 7. 一个窗口关闭时关闭几个窗口
- 8. 如何在关闭另一个窗口后打开一个弹出窗口?
- 9. 触发事件从另一个WPF窗口WPF窗口
- 10. 打开一个窗口,关闭一个窗口在PyQt5
- 11. 当一个人正在关闭/关闭时从另一个窗体绘制另一个窗体
- 12. 自动关闭一个vtk窗口befor另一个vtk窗口打开
- 13. 从另一个元素关闭Bootstrap的弹出窗口
- 14. 从另一个窗口打开WPF窗口
- 15. 如何从一个WPF窗口将类数组返回到另一个窗口?
- 16. 如何将WPF UserControl从一个窗口移动到另一个窗口?
- 17. 在WPF中关闭一个窗口(当不在该窗口中时)
- 18. 从另一个窗口
- 19. 如何从一个java关闭窗口中返回一个值?
- 20. 关闭实际窗口并从ViewModel打开一个新窗口
- 21. 关闭WPF窗口
- 22. 另一个WPF窗口前面的WPF窗口
- 23. WPF在不关闭原始窗口的情况下打开一个新窗口
- 24. WPF无法隐藏另一个窗口
- 25. 在另一个内部显示一个WPF窗口
- 26. 关闭一个窗体并打开另一个窗体
- 27. ASP.NET从另一个窗口刷新一个窗口
- 28. 将消息从一个窗口传递到另一个窗口
- 29. c#试图让一个事件在另一个窗口关闭时触发
- 30. 用一个按钮关闭弹出窗口(在另一个.xib文件中)
: - Good Work。其实我想从MainWindow关闭Window2。提前致谢。 – 2012-07-28 05:34:36
@AnoopMohan通过创建表单的表单来封闭表单要容易得多,您只需保留对创建表单的引用并在其上调用Close方法即可。 – 2012-07-28 05:47:59
:谢谢,先生。 。现在我可以关闭窗口。但我需要做更多的事情..我想关闭窗口并在同一事件中打开具有不同值的同一个窗口。对不起,打扰您。 。提前致谢。 。 – 2012-07-28 05:59:52