在silverlight实际项目中时常会需要播放视频和打印文档,在本节中我们将制作一个最简单的播放视频和打印文档的实例。
一、播放WMV视频
首先我们创建一个Silverlight应用程序SLShowVIDeo,然后放一个示例Wmv视频在SLShowVIDeo.web项目的根目录下面。
然后我们在Xaml文档中放入一个MediaElement控件,并写入以下代码:
<MediaElement Height="377" HorizontalAlignment="left" margin="8,31,0" name="showVIDeo" VerticalAlignment="top" WIDth="583" /> <button Content="播 放" Height="28" HorizontalAlignment="left" margin="194,428,0" name="button1" VerticalAlignment="top" WIDth="99" Click="button1_Click_1" /> <button Content="暂 停" Height="28" HorizontalAlignment="left" margin="333,0" name="button3" VerticalAlignment="top" WIDth="99" Click="button3_Click" /> <button Content="停 止" Height="28" HorizontalAlignment="left" margin="468,0" name="button2" VerticalAlignment="top" WIDth="99" Click="button2_Click" /> <button Content="加载视频" Height="28" HorizontalAlignment="left" margin="57,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" 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" 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的到来。下面我们看实例的效果图:
总结以上是内存溢出为你收集整理的36.Silverlight中播放视频和打印文档【附带源码实例】全部内容,希望文章能够帮你解决36.Silverlight中播放视频和打印文档【附带源码实例】所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)