微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Silverlight 4 新特性之NotificationWindow

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

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" 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"  Name="SendMessage" VerticalAlignment="Top" Width="151"  />  
  4.          <TextBlock Height="35" HorizontalAlignment="Left" Margin="31,23,0" Name="textBlock1" Text="Silverlight4 -NotificationWindow " VerticalAlignment="Top" Width="336" Foreground="Red" FontFamily="Lucida Sans Unicode" FontSize="20" />  
  5.      </Grid> 

<2>后台编码:

 
 
  1. public MainPage()  
  2.          {  
  3.              InitializeComponent();  
  4.              //绑定事件  
  5.              this.SendMessage.Click += new RoutedEventHandler(SendMessage_Click);  
  6.          }  
  7.  
  8.          void SendMessage_Click(object sender, RoutedEventArgs e)  
  9.          {  
  10.              //首先检测是否Application 在OOB状态  
  11.              if (Application.Current.IsRunningOutOfbrowser)  
  12.              {  
  13.                  NotificationWindow newwindow = new NotificationWindow();  
  14.                  newwindow.Height = 100;  
  15.                  newwindow.Width = 400;  
  16.                  newwindow.Closed += new EventHandler(newwindow_Closed);  
  17.    
  18.                  //设置内容  
  19.                  TextBlock msgblock = new TextBlock { Text = this.textBox1.Text, FontSize = 12};  
  20.    
  21.                  //尝试一下展示图片  
  22.                  Image img = new Image();  
  23.                  img.source = new BitmapImage(new Uri("../Images/danzeer.jpg", UriKind.RelativeOrAbsolute));  
  24.                  img.Width = 50;  
  25.                  img.Height = 50;  
  26.    
  27.                  Grid contentgrid = new Grid();  
  28.                  //contentgrid.VerticalAlignment = VerticalAlignment.Top;  
  29.                  //contentgrid.HorizontalAlignment = HorizontalAlignment.Left;  
  30.                    
  31.                  contentgrid.Children.Add(msgblock);  
  32.                  contentgrid.Children.Add(img);  
  33.                  newwindow.Content = contentgrid;  
  34.    
  35.                  //展示  
  36.                  newwindow.Show(5000);//展示3秒钟  
  37.    
  38.              }  
  39.              else 
  40.              {  
  41.                  MessageBox.Show("尚未初始化为OOB模式!");  
  42.              }  
  43.          }  
  44.  
  45.          void newwindow_Closed(object sender, EventArgs e)  
  46.          {  
  47.              MessageBox.Show("这是我的关闭事件!");  
  48.          } 

后台代码就能看出  首先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 实现代码见附件。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐