Silverlight 4 新特性之NotificationWindow

Silverlight4 中增加很多新特性. 相对于Silverlight 3 有了更加丰富运用. 当然对于用户而言也就有了更多的选择. 最近在更新 Silverlight 3版本时项目中关于消息通知的功能. . 原来项目中运用一个很成熟的JS库来实现消息通知. 样式比较单一. 而且更改起来比较麻烦. 而Silverlight 4 NotificationWindow 的出现则大大简化这个调用流程 .实现效果如下:

  Get Microsoft Silverlight

 A:Silverlight 4 NotificationWindow 只在OOB模式下支持 装载本地:

B:运行效果:

C:输入认识字符  点击Button按钮 在屏幕右下角:

 

实现步骤:

<1>页面布局-[很简单]:

1      < Grid  x:Name ="LayoutRoot"  Background ="White"  HorizontalAlignment ="Center"  VerticalAlignment ="Center" >
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>后台编码:

ExpandedBlockStart.gif 代码
 1         public  MainPage()
 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          }
48
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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值