36.Silverlight中播放视频和打印文档【附带源码实例】

简介:

    在silverlight实际项目中时常会需要播放视频和打印文档,在本节中我们将制作一个最简单的播放视频和打印文档的实例。

        一、播放WMV视频

        首先我们创建一个Silverlight应用程序SLShowVideo,然后放一个示例Wmv视频在SLShowVideo.web项目的根目录下面。

        然后我们在Xaml文档中放入一个MediaElement控件,并写入以下代码:

 

 
  1. <MediaElement Height="377" HorizontalAlignment="Left" Margin="8,31,0,0" 
  2. Name="showVideo" VerticalAlignment="Top" Width="583" /> 
  3. <Button Content="播 放" Height="28" HorizontalAlignment="Left" Margin="194,428,0,0" 
  4. Name="button1" VerticalAlignment="Top" Width="99" Click="button1_Click_1" /> 
  5. <Button Content="暂 停" Height="28" HorizontalAlignment="Left" Margin="333,428,0,0" 
  6. Name="button3" VerticalAlignment="Top" Width="99" Click="button3_Click" /> 
  7. <Button Content="停 止" Height="28" HorizontalAlignment="Left" Margin="468,428,0,0" 
  8. Name="button2" VerticalAlignment="Top" Width="99" Click="button2_Click" /> 
  9. <Button Content="加载视频" Height="28" HorizontalAlignment="Left" Margin="57,428,0,0" 
  10. Name="button4" VerticalAlignment="Top" Width="99" Click="button1_Click"/> 

        最后我们写入以下代码加载视频并且控制视频的播放:

 

 
  1. #region 播放视频 
  2. private void button1_Click(object sender, RoutedEventArgs e) 
  3. //加载视频 
  4. this.showVideo.Source = new Uri(GetURL()+"/sampleVideo.wmv"); 
  5. /// <summary> 
  6. /// 获取当前网站的Url前缀 
  7. /// </summary> 
  8. /// <returns></returns
  9. public static string GetURL() 
  10. ScriptObject location = (HtmlPage.Window.GetProperty("location"as ScriptObject); 
  11. object r = location.GetProperty("href"); 
  12. string URL = r.ToString().Substring(0, r.ToString().LastIndexOf('/')); 
  13. //截取到当前SILVERLIGHT程序存放网络URL的前缀 
  14. return URL; 
  15. private void button3_Click(object sender, RoutedEventArgs e) 
  16. //暂停 
  17. this.showVideo.Pause(); 
  18.  
  19. private void button2_Click(object sender, RoutedEventArgs e) 
  20. //停止 
  21. this.showVideo.Stop(); 
  22.  
  23. private void button1_Click_1(object sender, RoutedEventArgs e) 
  24. //播放 
  25. this.showVideo.Play(); 
  26. #endregion 

        二、打印文档

        首先我们看XAML文档。添加一个Canvas元素,元素内的所有内容就是我们即将要打印(当然你也可以设置打印Grid等元素的内容)。

 
  1. <Canvas Height="376" HorizontalAlignment="Left" Margin="611,32,0,0" Name="canvas1" 
  2. VerticalAlignment="Top" Width="369" > 
  3. <sdk:Label Width="85" Canvas.Left="9" Content="第一个打印程序" Canvas.Top="27" /> 
  4. <sdk:Label Canvas.Left="11" Canvas.Top="60" Height="16" Content="第二个打印程序" 
  5. Name="label1" Width="86" /> 
  6. </Canvas> 
  7. <Button Content="打印" Height="34" HorizontalAlignment="Left" Margin="747,426,0,0" 
  8. Name="btnPrint" VerticalAlignment="Top" Width="110" Click="btnPrint_Click" /> 

        在Button事件处理程序中我们添加一下代码打印Canvas元素。

 
  1. #region 打印文档 
  2. PrintDocument print; 
  3. private void btnPrint_Click(object sender, RoutedEventArgs e) 
  4. print = new PrintDocument(); 
  5. //添加一个打印页面事件以设置需要打印的控件 
  6. print.PrintPage += new EventHandler<PrintPageEventArgs>(print_PrintPage); 
  7. print.Print("canvas1"); 
  8.  
  9. void print_PrintPage(object sender, PrintPageEventArgs e) 
  10. //设置打印this.canvas1的所有内容 
  11. e.PageVisual = this.canvas1; 
  12. #endregion 

        本实例采用VS2010+Silverlight 4.0编写。如需源码请点击  SLShowVideo.zip  下载。期待Silverlight 5 beta的到来。下面我们看实例的效果图:


本文转自程兴亮 51CTO博客,原文链接:http://blog.51cto.com/chengxingliang/822567


相关文章

热门文章

最新文章

相关实验场景

更多