Chinaunix首页 | 论坛 | 博客
  • 博客访问: 497156
  • 博文数量: 105
  • 博客积分: 2922
  • 博客等级: 少校
  • 技术积分: 1113
  • 用 户 组: 普通用户
  • 注册时间: 2008-07-02 16:30
文章分类

全部博文(105)

文章存档

2018年(1)

2016年(2)

2015年(3)

2014年(6)

2013年(21)

2012年(10)

2011年(8)

2010年(7)

2009年(31)

2008年(16)

我的朋友

分类: 嵌入式

2012-04-14 12:29:14

1. Closing all other Windows when closing a main window

Normally in an application your have a main window that does some essential functionality. An example could be your web browser. The browser window is the main window and you may have an extra window which you can manage your bookmarks.

A quick and easy way i found to do this was create an event handler for the “Closing” event for the main window:

Closing="Window_Closing"

 

In the event handler we do this:

private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e) 
{ 
       
foreach (Window win in App.Current.Windows) 
       
{ 
           
if (win != this) 
           
{ 
                 win
.Close(); 
           
} 
       
} 
}

 

The real gem here is the App.Current.Windows collection, which does what it says on the tin and has a collection of all the windows that have been created in your application. An important part of the snippet above is that we make sure to not close the Window that the code belongs to otherwise you get an Exception, trust me i tried it!

阅读(4583) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~