wodehuajianrui

博客园 首页 新随笔 联系 订阅 管理
众所周知,在微软的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):

Explorer类与Form类的使用也相对简单,Explorer只需要写在Xaml里面,一般使其成为Application.Current.RootVisual的根元素,使其能够充满整个Silverlight应用即可。Form类大约可以像Silverlight3中推出的ChildWindow一样使用,只不过在Show之前需要设置它的Explorer属性,不然的话会出现空指针。

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

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