Chinaunix首页 | 论坛 | 博客
  • 博客访问: 496993
  • 博文数量: 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:32:14

2.  Open an Existing instance of a Window instead of a new Instance

Picture the scene, your creating an address book application and to add a new contact you have set it to bring up a new Add Contact Window. Once you finish you application and release it out in the wild you get a angry email from Timmy, a frustrated user. For some reason hes got 53 instances of the Add Contact Window open!

Now to keep poor Timmy happy what you need to do is before opening a new instance of your Window you want to make sure that none are already open.

And using the same magic as above you do the following:

foreach(Window win in App.Current.Windows) 
{ 
   
if (win.GetType() == typeof(propertiesWindow)) 
   
{ 
        win
.Show(); 
        win
.Focus(); 
       
return; 
   
} 
}

 

What we are doing is looping through the collection of Windows within your application and then checking if any of the windows are of the type we are wanting to open. If it does find one with the same type it will show the Window to the screen, if not it will do nothing.

To quote “Simples”.

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