众所周知,在微软的Windows平台,我们有许许多多的桌面应用,这让我们的生活变得丰富多彩而且有趣。在BS结构应用和RIA应用大行其道的今天,有很多桌面版应用推出了ajax版本和flex版本,免安装的同时也让升级和维护变得更加容易了。
Silverlight作为RIA的一种,它的优势体现在丰富的交互性上。如果说桌面版应用的载体是Windows Explorer的话,那么Silverlight应用的载体就是Internet Explorer(用FF的同学不要骂我,我还是喜欢IE的,因为它对SL的支持更好一些)。过去的一段时间,我通过补充和修改一些SL2时代写的代码,把Explorer这个概念搬到了Silverlight上,以求摸拟WPF上Window这个类的特性。由于本人的美术功底不好没有艺术细胞,而且也不怎么会用Blend,所以做出来的UI实在是有点不太好看。不过至少基本的功能都实现得差不多了,在这里我简要地介绍一下。
对于这个项目,主要的功能几乎全部由Explorer和Form这两个Control实现,前者可以定制WallPaper, Desktop Shortcuts, TaskBar等等,并且对所有的Form对象进行统一管理;后者主要用以支持最大化、还原、最小化,拖拽,改变窗口大小和窗口位置等逻辑功能。运行时的效果大约如图所示(这个是我自己的山寨版MSN):
My Silverlight系列(13)—— Silverlight Windows
Explorer类与Form类的使用也相对简单,Explorer只需要写在Xaml里面,一般使其成为Application.Current.RootVisual的根元素,使其能够充满整个Silverlight应用即可。Form类大约可以像Silverlight3中推出的ChildWindow一样使用,只不过在Show之前需要设置它的Explorer属性,不然的话会出现空指针。

在这个基础上实现一个自己的Form很简单,由于Form本身是一个ContentControl,那么在Xaml中可以这样定义:
My Silverlight系列(13)—— Silverlight Windows<provider:Form x:Class="SilverlightWindow.HelloWorld"
My Silverlight系列(13)—— Silverlight Windows    xmlns
="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
My Silverlight系列(13)—— Silverlight Windows    xmlns:x
="http://schemas.microsoft.com/winfx/2006/xaml" 
My Silverlight系列(13)—— Silverlight Windows    xmlns:provider
="clr-namespace:WinFormProvider.Controls;assembly=WinFormProvider"
My Silverlight系列(13)—— Silverlight Windows    Width
="600" Height="400" Icon="icon.jpg" Title="Hello World" Top="100" Left="100"
My Silverlight系列(13)—— Silverlight Windows    ShowInTaskBar
="True" MinWidth="500" MinHeight="300">
My Silverlight系列(13)—— Silverlight Windows    
<provider:Form.TrayAreaIcon>
My Silverlight系列(13)—— Silverlight Windows        
<provider:TrayAreaIcon Icon="icon.jpg" DoubleClick="TrayAreaIcon_DoubleClick"/>    
My Silverlight系列(13)—— Silverlight Windows    
</provider:Form.TrayAreaIcon>
My Silverlight系列(13)—— Silverlight Windows    
<Grid x:Name="LayoutRoot" Background="White">
My Silverlight系列(13)—— Silverlight Windows        
<TextBlock>
My Silverlight系列(13)—— Silverlight Windows            Hello, there is a form, it will show with 100px XOffset and 100px YOffset
My Silverlight系列(13)—— Silverlight Windows            
<LineBreak/>
My Silverlight系列(13)—— Silverlight Windows            You can resize it, and drag it, or close it
My Silverlight系列(13)—— Silverlight Windows            
<LineBreak/>
My Silverlight系列(13)—— Silverlight Windows            I don't konw how to create an introduction documentation
My Silverlight系列(13)—— Silverlight Windows            
<LineBreak/>
My Silverlight系列(13)—— Silverlight Windows            However, I think this control is quite easy, you can do it as WPF window
My Silverlight系列(13)—— Silverlight Windows            
<LineBreak/>   
My Silverlight系列(13)—— Silverlight Windows            
<LineBreak/>
My Silverlight系列(13)—— Silverlight Windows            
<LineBreak/>
My Silverlight系列(13)—— Silverlight Windows            And don't be surprised if the footer disappers from taskbar when you minimize the form
My Silverlight系列(13)—— Silverlight Windows            
<LineBreak/>
My Silverlight系列(13)—— Silverlight Windows            it is because I think if there is a trayarea icon, footer is useless
My Silverlight系列(13)—— Silverlight Windows        
</TextBlock>
My Silverlight系列(13)—— Silverlight Windows    
</Grid>
My Silverlight系列(13)—— Silverlight Windows
</provider:Form>
My Silverlight系列(13)—— Silverlight Windows
而且后台的代码中,由于开放了比较丰富的public、protected属性、方法以及事件,因此我们可以实现很多功能,具体您可以参考代码以及示例。

这个项目(包括示例)我已经将它发布到了codeplex上面:http://silverlightwindow.codeplex.com/,是一个Alpha版本。如果哪位有兴趣的话,可以联系我,大家一起把它完善了。
我仔细阅读了有关把文章放到首页的要求,感觉我这篇随笔像是属于4) 软件发布的文章,但是我希望能有更多的人看到这篇文章,如果您喜欢silverlight,觉得这个项目有价值的话,欢迎加入我,大家一起丰富和完善这个项目。
最后再小小庆祝一下本人在silverlight官网(http://silverlight.net/forums)的积分达到2000,成功晋级contributor。截图留念:
My Silverlight系列(13)—— Silverlight Windows  

相关文章:

  • 2021-12-14
  • 2022-12-23
  • 2021-07-18
  • 2022-12-23
  • 2021-05-27
  • 2021-12-26
  • 2021-12-18
  • 2021-12-21
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-18
  • 2021-06-30
相关资源
相似解决方案