热门:网页模板.net视频教程JQueryMVCjsonExtJs源码示例三级联动JQuery菜单
您现在的位置:.Net中文社区>> Silverlight>>正文内容

Silverlight读取Zip文件中的图片与视频【附源码示例】

发布时间:2010年08月17日点击数: 佚名

其实在我的 WebClient的使用文章中的例子就是如何使用WebClient读取zip文件中的资源,这篇文章是在其基础上增加了一些功能,从而构建出一个简单但较为完整的Demo。

    首先看看Demo的截图:【示例源码

                      2 3 1

   下面我将一步步展示实现这个Demo的过程,这个需求就是读出Zip文件中的图片与视频。

    Demo整体架构:

                          捕获

   

    首先我们准备几张图片和视频,然后将其压缩至resource.zip文件中,做完之后,我们建立一个resource.xml文件记录压缩包内的资源

  1. <?xml version="1.0" encoding="utf-8" ?>  
  2.       <files> 
  3.       <file type="video" name="a.wmv"/> 
  4.       <file type="image" name="1.jpg"/> 
  5.       <file type="image" name="2.jpg"/> 
  6.       <file type="image" name="3.jpg"/> 
  7.       <file type="image" name="4.jpg"/> 
  8.       </files> 

这个xml文件就记录了文件的类型和名称,完成之后我们将其压缩至resource.zip中(请注意:这一步对后面读取流会有影响)
    现在我们将UI设计好

  1. <Image x:Name="Image" /> 
  2.          <MediaElement x:Name="Video" /> 
  3.          <StackPanel HorizontalAlignment="Left" VerticalAlignment="Bottom" Opacity="0.5" Orientation="Horizontal" Margin="5,0,0,0" Name="stack"> 
  4.             <Button Content="Prev" x:Name="PrevButton"  Height="30" Margin="0,0,5,0" Width="80" Opacity="1" Cursor="Hand" IsEnabled="False"  /> 
  5.             <Button  x:Name="NextButton" Height="30" Margin="0,0,5,0" Width="80" Opacity="1" Content="Next" Cursor="Hand" IsEnabled="False"  /> 
  6.          </StackPanel> 

在UI上放置了一个Image和MediaElement控件来显示读取的资源
    下面我们开始建立ResourceInfo.cs文件

  1. public enum ResourceType 
  2.       { 
  3.         Video, 
  4.         Image 
  5.       } 
  6.       public class ResourceInfo 
  7.       { 
  8.         public ResourceType Type 
  9.         { 
  10.             get;  set
  11.         } 
  12.         public string Name 
  13.         { 
  14.             getset
  15.         } 
  16.        } 

文件的类型以枚举的形式表示,现在我们就在MainPage.xaml.cs中以WebClient完成数据的读取工作
     先声明3个类成员变量:

  1. private StreamResourceInfo zip; 
  2.        private List<ResourceInfo> resourceInfo; 
  3.        private int index = 0; 

现在我们就先获取流,其完整代码:

  1. public void Load(object sender,RoutedEventArgs e) 
  2.        { 
  3.            Uri uri = new Uri(HtmlPage.Document.DocumentUri, "resource.zip"); 
  4.            WebClient webClient = new WebClient(); 
  5.            webClient.OpenReadCompleted += (obj, args) =>  
  6.            { 
  7.                if (args.Error != null
  8.                { 
  9.                    return
  10.                } 
  11.                // 这几步将读出的流信息封装到reader中,这样便于后面使用Linq To Xml操作 
  12.                zip = new StreamResourceInfo(args.Result, null); 
  13.                StreamResourceInfo maininfo = Application.GetResourceStream(zip, new Uri("resource.xml", UriKind.Relative)); 
  14.                StreamReader reader = new StreamReader(maininfo.Stream); 
  15.                XDocument doc = XDocument.Load(reader); 
  16.                var file = from c in doc.Descendants("file"
  17.                           select new ResourceInfo 
  18.                           { 
  19.                               Type = (ResourceType)Enum.Parse(typeof(ResourceType), c.Attribute("type").Value, true), 
  20.                               Name = c.Attribute("name").Value 
  21.                           }; 
  22.                resourceInfo = new List<ResourceInfo>(); 
  23.                resourceInfo.AddRange(file); 
  24.                this.PrevButton.IsEnabled = true
  25.                this.NextButton.IsEnabled = true
  26.                Display(resourceInfo[0]); 
  27.            }; 
  28.             webClient.OpenReadAsync(uri); 
  29.        } 
  30.         public void Display(ResourceInfo resource) 
  31.        { 
  32.            //获取相应的流数据 
  33.            StreamResourceInfo media = Application.GetResourceStream(zip,new Uri(resource.Name,UriKind.Relative)); 
  34.            switch (resource.Type) 
  35.            { 
  36.                case ResourceType.Image: 
  37.                    Image.Visibility = Visibility.Visible; 
  38.                    Video.Visibility = Visibility.Collapsed; 
  39.                    BitmapImage image = new BitmapImage(); 
  40.                    image.SetSource(media.Stream); 
  41.                    Image.Source = image; 
  42.                    break
  43.                case ResourceType.Video: 
  44.                    Image.Visibility = Visibility.Collapsed; 
  45.                    Video.Visibility = Visibility.Visible; 
  46.                    Video.SetSource(media.Stream); 
  47.                    Video.Play(); 
  48.                    break
  49.            } 
  50.         } 

 事实上加载这段代码后,我们已经可以将xml文件中标注的第一个资源a.wmv在页面进行成功的播放了
          我们继续界面上的Button实现的循环显示上一条,下一条资源功能

  1. private void StopVideo() 
  2.         { 
  3.             if (resourceInfo[index].Type == ResourceType.Video) 
  4.             { 
  5.                 Video.Stop(); 
  6.             } 
  7.         } 
  8.         private void PrevButton_Click(object sender, RoutedEventArgs e) 
  9.         { 
  10.             StopVideo(); 
  11.             if (--index < 0) 
  12.             { 
  13.                 index = resourceInfo.Count - 1; 
  14.             } 
  15.             Display(resourceInfo[index]); 
  16.         } 
  17.         private void NextButton_Click(object sender, RoutedEventArgs e) 
  18.         { 
  19.             StopVideo(); 
  20.             if (++index >=resourceInfo.Count) 
  21.             { 
  22.                 index = 0; 
  23.             } 
  24.             Display(resourceInfo[index]); 
  25.         } 

本站热点业务

更多模板/案例展示

关于我们 | 联系我们 | 团队日志 | 网站地图 | 网站合作