Silverlight4 中增加很多新特性. 相对于Silverlight 3 有了更加丰富运用. 当然对于用户而言也就有了更多的选择. 最近在更新 Silverlight 3版本时项目中关于消息通知的功能. . 原来项目中运用一个很成熟的JS库来实现消息通知. 样式比较单一. 而且更改起来比较麻烦. 而Silverlight 4 NotificationWindow 的出现则大大简化这个调用流程 .实现效果如下:
A:Silverlight 4 NotificationWindow 只在OOB模式下支持 装载本地:
B:运行效果:
C:输入认识字符 点击Button按钮 在屏幕右下角:
实现步骤:
<1>页面布局-[很简单]:
2 < TextBox Height ="68" HorizontalAlignment ="Left" Margin ="12,75,0,0" Name ="textBox1" VerticalAlignment ="Top" Width ="441" TextWrapping ="Wrap" IsTabStop ="True" FontSize ="12" />
3 < Button Content ="Give me The Message" Foreground ="Red" Background ="Red" Height ="50" HorizontalAlignment ="Left" Margin ="488,84,0,0" Name ="SendMessage" VerticalAlignment ="Top" Width ="151" />
4 < TextBlock Height ="35" HorizontalAlignment ="Left" Margin ="31,23,0,0" Name ="textBlock1" Text ="Silverlight4 -NotificationWindow " VerticalAlignment ="Top" Width ="336" Foreground ="Red" FontFamily ="Lucida Sans Unicode" FontSize ="20" />
5 </ Grid >
<2>后台编码:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
2 {
3 InitializeComponent();
5 // 绑定事件
6 this .SendMessage.Click += new RoutedEventHandler(SendMessage_Click);
7 }
8
9 void SendMessage_Click( object sender, RoutedEventArgs e)
10 {
11 // 首先检测是否Application 在OOB状态
12 if (Application.Current.IsRunningOutOfBrowser)
13 {
14 NotificationWindow newwindow = new NotificationWindow();
15 newwindow.Height = 100 ;
16 newwindow.Width = 400 ;
17 newwindow.Closed += new EventHandler(newwindow_Closed);
18
19 // 设置内容
20 TextBlock msgblock = new TextBlock { Text = this .textBox1.Text, FontSize = 12 };
21
22 // 尝试一下展示图片
23 Image img = new Image();
24 img.Source = new BitmapImage( new Uri( " ../Images/danzeer.jpg " , UriKind.RelativeOrAbsolute));
25 img.Width = 50 ;
26 img.Height = 50 ;
27
28 Grid contentgrid = new Grid();
29 // contentgrid.VerticalAlignment = VerticalAlignment.Top;
30 // contentgrid.HorizontalAlignment = HorizontalAlignment.Left;
31
32 contentgrid.Children.Add(msgblock);
33 contentgrid.Children.Add(img);
34 newwindow.Content = contentgrid;
35
36 // 展示
37 newwindow.Show( 5000 ); // 展示3秒钟
38
39 }
40 else
41 {
43 MessageBox.Show( " 尚未初始化为OOB模式! " );
44 }
47 }
49 void newwindow_Closed( object sender, EventArgs e)
50 {
51 MessageBox.Show( " 这是我的关闭事件! " );
52 }
从后台代码就能看出 首先new 一个NotificationWindow, 设置宽高属性和内容. 然后通过Show(“显示毫秒数”)方法显示出来 .出现在屏幕右下角 实现消息通知功能.
<3>注意事项:
其实看到这个NotificationWindow 就想到XP系统托盘通知效果基本类似. 在Silverlight也能做成这样的效果. 关键在于对NotificationWindow 用户自定义美化
(1)NotificationWindow 宽度和高度目前是固定设置 400X100. 既宽度:400 高度:100 必须设置固定当前值.
(2)NotificationWindow 支持用户交互. 但目前只支持鼠标操作.
(3)因为Silverlight暂时还没有消息队列来控制. 所以一次无法同时显示多个NotificationWindow .
(4)默认的NotificationWindow 有点粗糙. SL 支持用户自定义NotificationWindow 关于具体相关属性和操作 资料请参见MSDN NotificationWindow类
Silverlight 4 NotificationWindow 实现: /Files/chenkai/sl/TestNotificationWindowDemo.rar