首先看看Demo的截图:

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

Demo整体架构:

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

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

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

现在我们将UI设计好

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

在UI上放置了一个Image和MediaElement控件来显示读取的资源

下面我们开始建立ResourceInfo.cs文件

       public enum ResourceType
      {
        Video,
        Image
      }
      public class ResourceInfo
      {
        public ResourceType Type
        {
            get;  set;
        }
        public string Name
        {
            get; set;
        }
       }

文件的类型以枚举的形式表示,现在我们就在MainPage.xaml.cs中以WebClient完成数据的读取工作

先声明3个类成员变量:

       private StreamResourceInfo zip;
       private List<ResourceInfo> resourceInfo;
       private int index = 0;

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

       public void Load(object sender,RoutedEventArgs e)
        {
            Uri uri = new Uri(HtmlPage.Document.DocumentUri, "resource.zip");
            WebClient webClient = new WebClient();
            webClient.OpenReadCompleted += (obj, args) =>
            {
                if (args.Error != null)
                {
                    return;

                }
                // 这几步将读出的流信息封装到reader中,这样便于后面使用Linq To Xml操作
                zip = new StreamResourceInfo(args.Result, null);
                StreamResourceInfo maininfo = Application.GetResourceStream(zip, new Uri("resource.xml", UriKind.Relative));
                StreamReader reader = new StreamReader(maininfo.Stream);

                XDocument doc = XDocument.Load(reader);
                var file = from c in doc.Descendants("file")
                           select new ResourceInfo
                           {
                               Type = (ResourceType)Enum.Parse(typeof(ResourceType), c.Attribute("type").Value, true),
                               Name = c.Attribute("name").Value
                           };
                resourceInfo = new List<ResourceInfo>();
                resourceInfo.AddRange(file);
                this.PrevButton.IsEnabled = true;
                this.NextButton.IsEnabled = true;
                Display(resourceInfo[0]);
            };
             webClient.OpenReadAsync(uri);
        }
         public void Display(ResourceInfo resource)
        {
            //获取相应的流数据
            StreamResourceInfo media = Application.GetResourceStream(zip,new Uri(resource.Name,UriKind.Relative));
            switch (resource.Type)
            {
                case ResourceType.Image:
                    Image.Visibility = Visibility.Visible;
                    Video.Visibility = Visibility.Collapsed;
                    BitmapImage image = new BitmapImage();
                    image.SetSource(media.Stream);
                    Image.Source = image;
                    break;
                case ResourceType.Video:
                    Image.Visibility = Visibility.Collapsed;
                    Video.Visibility = Visibility.Visible;
                    Video.SetSource(media.Stream);
                    Video.Play();
                    break;
            }
         }

事实上加载这段代码后,我们已经可以将xml文件中标注的第一个资源a.wmv在页面进行成功的播放了

我们继续界面上的Button实现的循环显示上一条,下一条资源功能

         private void StopVideo()
        {
            if (resourceInfo[index].Type == ResourceType.Video)
            {
                Video.Stop();
            }
        }
        private void PrevButton_Click(object sender, RoutedEventArgs e)
        {
            StopVideo();
            if (--index < 0)
            {
                index = resourceInfo.Count - 1;
            }
            Display(resourceInfo[index]);
        }
        private void NextButton_Click(object sender, RoutedEventArgs e)
        {
            StopVideo();
            if (++index >=resourceInfo.Count)
            {
                index = 0;
            }
            Display(resourceInfo[index]);
        }

如此我们就完成了这个Demo

Silverlight读取Zip文件中的图片与视频的更多相关文章

  1. java 读取Zip文件进行写入

    直接读取ZIp文件读取写入到别的文件中. package jp.co.misumi.mdm.batch; import java.io.BufferedReader; import java.io.F ...

  2. SpringMVC 实现POI读取Excle文件中数据导入数据库(上传)、导出数据库中数据到Excle文件中(下载)

    读取Excale表返回一个集合: package com.shiliu.game.utils; import java.io.File; import java.io.FileInputStream; ...

  3. 读取Excel文件中的单元格的内容和颜色

    怎样读取Excel文件中的单元格的内容和颜色 先创建一个Excel文件,在A1和A2中随意输入内容,设置A1的字体颜色为红色,A2的背景为黄色.需要 using Excel = Microsoft.O ...

  4. java 中读取本地文件中字符

    java读取txt文件内容.可以作如下理解: 首先获得一个文件句柄.File file = new File(); file即为文件句柄.两人之间连通电话网络了.接下来可以开始打电话了. 通过这条线路 ...

  5. Java中读取txt文件中中文字符时,出现乱码的解决办法

    这是我写的一个Java课程作业时,遇到的问题. 问题描述: 我要实现的就是将txt文件中的内容按一定格式读取出来后,存放在相应的数组. 我刚开始运行时发现,英文可以实现,但是中文字符就是各种乱码. 最 ...

  6. Flex读取txt文件中的内容(三)

    Flex读取txt文件中的内容 1.设计源码 LoadTxt.mxml: <?xml version="1.0" encoding="utf-8"?> ...

  7. Flex读取txt文件中的内容(二)

    Flex读取txt文件中的内容 自动生成的文件 LoadTxt-app.xml: <?xml version="1.0" encoding="utf-8" ...

  8. Flex读取txt文件中的内容(一)

    Flex读取txt文件中的内容 phone.txt: 13000003847 13000003848 13000003849 13000003850 13000003851 13000003852 1 ...

  9. Flex读取txt文件中的内容报错

    Flex读取txt文件中的内容 1.具体错误如下 2.错误原因 读取文件不存在 var file:File = new File(File.applicationDirectory.nativePat ...

随机推荐

  1. struts (一)

    1.jar 2.web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app id=&quo ...

  2. VisualVM 性能分析概述

    VisualVM是一个免费的Java应用监控.分析工具. 简单说来,VisualVM是一种集成了多个JDK命令行工具的可视化工具,它能为您提供强大的分析能力.所有这些都是免费的!它囊括的命令行工具包括 ...

  3. [SQL]当输入表达式得数为一个有效的整数、浮点数、money 或 decimal 类型,那么 ISNUMERIC 返回 1;否则返回 0

    ISNUMERIC ( expression ) 当输入表达式得数为一个有效的整数.浮点数.money 或 :否则返回 .返回值为 确保可以将 expression 转换为上述数字类型中的一种. 注意 ...

  4. Js字符串与十六进制的相互转换

    开发过程中,字符串与十六进.二进制之间的相互转换常常会用到,尤其是涉及到中文的加密时,就需要把中文转换为十六进制.下面说说具体的转换方法. 1.字符串转换为十六进制 主要使用 charCodeAt() ...

  5. MemoryStream 的GetBuffer() 和 ToArray()的区别

    GetBuffer(): Note that the buffer contains allocated bytes which might be unused. For example, if th ...

  6. css first-letter实现首字(字母)下沉效果

    css 首字下沉效果原理 首字下沉主要使用到css的first-letter伪元素,然后配合使用font-size和float来设置文字的样式即可实现. first-letter选择器选取指定元素文本 ...

  7. wmware10安装ghost win7问题处理

    随便找到了ghostwin7.iso, 先建立空的虚拟机, 加载iso, 按F2, 设置启动从光盘启动, 启动进去后点直安装Ghost镜像到C盘, 失盘, 直接跳到dos界面了. 忘记先要分区了, 使 ...

  8. dumpsys命令的使用及telephony.registry解读

    adb shell dumpsys,默认打印出当前系统所有的service信息,通常情况下我们并不想看那么多信息,可以在后面加上具体的服务名,比如想获取关于设备电池的信息,就可以使用以下命令: > ...

  9. JDK源码分析之集合03LinkedList

    一.前言 LinkedList是双向列表,实现方式是使用链表的方式保存元素:除了第一个和最后一个元素外,每一个节点都包含一个指向前一个和指向后一个节点的指针和元素的值.其特点是插入删除效率高,而随机访 ...

  10. OpenGL ES学习笔记(一)——基本用法、绘制流程与着色器编译

    首先声明下,本文为笔者学习<OpenGL ES应用开发实践指南(Android卷)>的笔记,涉及的代码均出自原书,如有需要,请到原书指定源码地址下载. 在Android.iOS等移动平台上 ...