熟悉Silverlight的朋友应该知道,Silverlight从1.0版本到现在的4.0版本,其功能性越来越强大,从下图我们可以看出,Silverlight的应用模型的一个转变过程,从Javascript到现在Trusted应用,我们目睹了Silverlight坎坷的演变过程,尽管现在仍旧存在不足之处,但是有了更多开发人员的支持和帮助,Silverlight一定会更好更强大。
在前几篇中,我们通过简单的实例详细介绍了Silverlight Out of browser应用开发基础。为了下一篇的实例做准备,本篇,我们将补充介绍一些Silverlight Out of browser应用开发知识点,
1. 回顾Silverlight Out of browser存取本地目录API;
2. 学习Silverlight Out of browser应用Debug调试;
3. 学习Silverlight Out of browser的消息通知窗口API;
回顾Silverlight Out of browser存取本地目录API,还记得在前面的文章我们已经介绍,Silverlight提供有现成的API,允许应用在OOB模式下访问My...系列目录,API调用方法很简单,而OOB模式下文件访问,应用可以支持System.IO引用空间中的操作类,例如File类,Directory类,Environment类,等等。
2 {
3 var path = Environment.GetFolderPath(Environment.SpecialFolder.MyMusic);
4 lsMyMusics.Items.Clear();
5 DirectoryInfo myDirectory new DirectoryInfo(path);
6 foreach (FileInfo file in myDirectory.EnumerateFiles())
7 {
8 lsMyMusics.Items.Add(file);
9 }
10 }
在下文中,我们将用到Silverlight默认API,读取My Music目录的音乐文件作为默认播放目录。其代码与上相似。
Silverlight Out of browser应用的调试方法(Debug)
在使用Silverlight开发应用时,Debug是最常用的Visual Studio工具之一。大家可能对Silverlight基于Web浏览器的调试方法并不陌生,终归ASP.NET应用开发调试已经为Silverlight打下了良好的基础。而对于Silverlight的OOB是否通常支持Debug呢?答案是肯定的。
在创建项目时,默认的设置,是支持基于浏览器的应用的Debug。如果需要OOB应用支持脱离浏览器进行Debug,需要按照以下几个步骤设置:
1. 首先需要设置Silverlight客户端应用为“开始项目”,
2. 然后选择客户端应用“Properties”属性栏,设置“Start Action”中的“Installed out-of-browser application”,
3. 点击保存后,重新F5调试应用,即可发现,应用将直接作为OOB模式启动,不再进入Web浏览器中提示用户安装,这时就可以在代码中设置断点进行Debug了。
学习Silverlight Out of browser的消息通知窗口API(Toast Notifications Windows)
Toast Notifications Windows,又称为Silverlight消息通知窗口,是Silverlight 4的一个新特性,该窗口目前仅限于Out of browser应用使用。该消息窗口主要是提供临时消息提示,在应用中可以起到让用户注意警示的作用。现在很多Windows应用都喜欢使用该窗口模式显示广告,更新提示等功能。
Silverlight的Notifications Windows目前有以下限制:
1. 窗口最大尺寸限制,最大仅支持宽400,高100的提示窗口;
2. 目前不支持Transparency窗口特效,WPF可以支持;
3. 为了区别其他窗口应用,Notifications Windows无窗口边框;
在明白以上限制后,使用Silverlight API很轻松就能创建一个Toast Notifications窗口。其方法如下:
首先,在SilverlightOOBDemo中创建一个NotificationControl控件,
编辑NotificationControl控件,我们简单的对该控件进行美化,使其看起来更加友好,
<
UserControl
x:Class
="SilverlightOOBDemo.NotificationControl"
2
xmlns
="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3
xmlns:x
="http://schemas.microsoft.com/winfx/2006/xaml"
4
xmlns:d
="http://schemas.microsoft.com/expression/blend/2008"
5
xmlns:mc
="http://schemas.openxmlformats.org/markup-compatibility/2006"
6
mc:Ignorable
="d"
7
d:DesignHeight
="300"
d:DesignWidth
="400"
>
Grid
x:Name
="LayoutRoot"
Background
="White"
Border
="Frame"
Width
Height
="100"
="LightYellow"
11
StackPanel
Orientation
="Vertical"
12
Width
="290"
="24"
CornerRadius
="4"
Margin
="2,4,2,4"
13
Border.Background
14
LinearGradientBrush
StartPoint
="0.5,0.0"
15
EndPoint
16
GradientStop
Offset
="0.2"
Color
="#FF1C68A0"
/>
17
="1.0"
="#FF54A7E2"
18
</
LinearGradientBrush
19
20
Border.Effect
21
DropShadowEffect
BlurRadius
ShadowDepth
22
Opacity
="0.4"
23
24
TextBlock
Text
="友情提示"
FontSize
="12"
25
FontWeight
="Bold"
Foreground
="White"
26
Border
27
="Horizontal"
28
Image
Source
="/SilverlightOOBDemo;component/Images/Update.png"
="48"
29
Stretch
="Fill"
VerticalAlignment
="Top"
30
="240"
Text
="检测到新的音乐文件,已经更新到播放列表。小广告:我的博客http://jv9.cnblogs.com"
31
FontSize
="11"
="#FF202020"
textwrapping
="Wrap"
32
Margin
33
StackPanel
34
35
36
Grid
37
UserControl
38