Silverlight实用窍门系列:36.Silverlight中播放视频和打印文档【附带源码实例】
2023-09-14 08:56:52 时间
在silverlight实际项目中时常会需要播放视频和打印文档,在本节中我们将制作一个最简单的播放视频和打印文档的实例。
一、播放WMV视频
首先我们创建一个Silverlight应用程序SLShowVideo,然后放一个示例Wmv视频在SLShowVideo.web项目的根目录下面。
然后我们在Xaml文档中放入一个MediaElement控件,并写入以下代码:
MediaElement Height="377" HorizontalAlignment="Left" Margin="8,31,0,0" Name="showVideo" VerticalAlignment="Top" Width="583" / Button Content="播 放" Height="28" HorizontalAlignment="Left" Margin="194,428,0,0" Name="button1" VerticalAlignment="Top" Width="99" Click="button1_Click_1" / Button Content="暂 停" Height="28" HorizontalAlignment="Left" Margin="333,428,0,0" Name="button3" VerticalAlignment="Top" Width="99" Click="button3_Click" / Button Content="停 止" Height="28" HorizontalAlignment="Left" Margin="468,428,0,0" Name="button2" VerticalAlignment="Top" Width="99" Click="button2_Click" / Button Content="加载视频" Height="28" HorizontalAlignment="Left" Margin="57,428,0,0" Name="button4" VerticalAlignment="Top" Width="99" Click="button1_Click"/最后我们写入以下代码加载视频并且控制视频的播放:
#region 播放视频 private void button1_Click(object sender, RoutedEventArgs e) //加载视频 this.showVideo.Source = new Uri(GetURL()+"/sampleVideo.wmv"); /// summary /// 获取当前网站的Url前缀 /// /summary /// returns /returns public static string GetURL() ScriptObject location = (HtmlPage.Window.GetProperty("location") as ScriptObject); object r = location.GetProperty("href"); string URL = r.ToString().Substring(0, r.ToString().LastIndexOf(/)); //截取到当前SILVERLIGHT程序存放网络URL的前缀 return URL; private void button3_Click(object sender, RoutedEventArgs e) //暂停 this.showVideo.Pause(); private void button2_Click(object sender, RoutedEventArgs e) //停止 this.showVideo.Stop(); private void button1_Click_1(object sender, RoutedEventArgs e) //播放 this.showVideo.Play(); #endregion
二、打印文档
首先我们看XAML文档。添加一个Canvas元素,元素内的所有内容就是我们即将要打印(当然你也可以设置打印Grid等元素的内容)。
Canvas Height="376" HorizontalAlignment="Left" Margin="611,32,0,0" Name="canvas1" VerticalAlignment="Top" Width="369" sdk:Label Width="85" Canvas.Left="9" Content="第一个打印程序" Canvas.Top="27" / sdk:Label Canvas.Left="11" Canvas.Top="60" Height="16" Content="第二个打印程序" Name="label1" Width="86" / /Canvas Button Content="打印" Height="34" HorizontalAlignment="Left" Margin="747,426,0,0" Name="btnPrint" VerticalAlignment="Top" Width="110" Click="btnPrint_Click" /
在Button事件处理程序中我们添加一下代码打印Canvas元素。
#region 打印文档 PrintDocument print; private void btnPrint_Click(object sender, RoutedEventArgs e) print = new PrintDocument(); //添加一个打印页面事件以设置需要打印的控件 print.PrintPage += new EventHandler PrintPageEventArgs (print_PrintPage); print.Print("canvas1"); void print_PrintPage(object sender, PrintPageEventArgs e) //设置打印this.canvas1的所有内容 e.PageVisual = this.canvas1; #endregion
本实例采用VS2010+Silverlight 4.0编写。如需源码请点击 SLShowVideo.zip 下载。期待Silverlight 5 beta的到来。下面我们看实例的效果图:
![2011041310223442.jpg](http://pic002.cnblogs.com/images/2011/140041/2011041310223442.jpg)
煦风满裳 程兴亮,专注于C#方面的开发工作,喜欢钻研Silverlight,CIL等方面的知识。平时喜欢看书、写技术博客、摄影、旅游和运动。
相关文章
- 【MySQL】MySQL多实例开机自动重启
- java面向对象高级分层实例_BaseDao
- Vue实例的生命周期(钩子函数)
- 转 php简单伪静态实例
- Spring 2.5 jar 所有开发包及完整文档及项目开发实例
- 显著性水平 置信度 置信区间 实例讲解
- MUX VLAN详解与配置实例
- ASP.NET Core 6框架揭秘实例演示[32]:错误页面的N种呈现方式
- ASP.NET Core 6框架揭秘实例演示[05]:依赖注入基本编程模式
- 关于 Angular 服务实例作用域的问题 - 使用组件限定服务提供者的作用域
- 如何把nodejs应用和SAP云平台上的Redis实例做绑定
- 探索static——不需要能够使用该类实例?
- Shell 脚本实例