惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

W
WeLiveSecurity
The GitHub Blog
The GitHub Blog
Engineering at Meta
Engineering at Meta
Microsoft Azure Blog
Microsoft Azure Blog
The Register - Security
The Register - Security
Stack Overflow Blog
Stack Overflow Blog
博客园 - 三生石上(FineUI控件)
T
Threat Research - Cisco Blogs
S
SegmentFault 最新的问题
V2EX - 技术
V2EX - 技术
Hacker News: Ask HN
Hacker News: Ask HN
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
P
Proofpoint News Feed
J
Java Code Geeks
Microsoft Security Blog
Microsoft Security Blog
M
MIT News - Artificial intelligence
AI
AI
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
P
Proofpoint News Feed
Hacker News - Newest:
Hacker News - Newest: "LLM"
B
Blog
N
News and Events Feed by Topic
N
News | PayPal Newsroom
Google DeepMind News
Google DeepMind News
酷 壳 – CoolShell
酷 壳 – CoolShell
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
WordPress大学
WordPress大学
C
Cybersecurity and Infrastructure Security Agency CISA
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
博客园 - 【当耐特】
U
Unit 42
腾讯CDC
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
The Cloudflare Blog
H
Help Net Security
Recent Announcements
Recent Announcements
P
Privacy & Cybersecurity Law Blog
IT之家
IT之家
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Security Archives - TechRepublic
Security Archives - TechRepublic
L
LINUX DO - 热门话题
Martin Fowler
Martin Fowler
MongoDB | Blog
MongoDB | Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
H
Heimdal Security Blog
博客园 - 聂微东
S
Securelist
大猫的无限游戏
大猫的无限游戏
Cloudbric
Cloudbric
Cisco Talos Blog
Cisco Talos Blog

博客园 - MobileGeek

Swift全栈开发 App Submission Issues 番茄钟App(Pomodoro Tracker) WatchKit App Submission Issues Watch​Kit Learning Resources Scrum Planning Card How to get cocoapods work on Yosemite 如何在DigitalOcean安装Ghost Swift学习资源 Xcode 6 模拟器路径 Android AutoCompleteTextView,Spinner和ListView的使用 Android Layout 布局 Android消息提示框和对话框 Silverlight Exception: “The URI prefix is not recognized.” “Unable to start debugging. The Silverlight Developer Runtime is not installed. Please install a matching version.” 解决办法 Android Project Structure(项目结构) Hello Android Introducing Android 使用Git和Repo获取Android源码
Silverlight4中使用WebClient类下载压缩成zip格式的图片
MobileGeek · 2010-09-12 · via 博客园 - MobileGeek

Silverlight与其它系统通信的方式主要有三种,通过WCF或者Web Service,HTTP直接通信和Socket通信。

HTTP直接通信可以使用WebClient类和HttpWebRequest类。WebClient提供了一种简单的基于事件的模型来让你以流或者字符串的方式下载和上传资源, 而HttpWebRequest提供了更复杂的通信场景,使用.NET异步编程模式来发送请求。

这里我使用WebClient类来下载压缩成zip格式的图片, 这也是Silverlight的一个优点,就是图片,视频等资源文件可以压缩成zip格式,然后通过WebClient类下载到客户端, 然后把zip文件里面的资源与相应的MediaElement或者Image关联起来就可以显示了。本例运行之后的效果如下:

image

点击Download按钮去下载zip文件,ListBox是硬编码绑定了几张图片列表,在ListBox中点击图片列表在右边的Image控件中显示相应的图片。

先看一下XAML代码:

<Grid x:Name="LayoutRoot" Background="White">
        <Button Content="Download" Height="25" HorizontalAlignment="Left" Margin="12,21,0,0" Name="downloadButton" VerticalAlignment="Top" Width="107" Click="downloadButton_Click" />
        <ListBox Name="imageListBox" Height="234" HorizontalAlignment="Left" Margin="12,54,0,0" VerticalAlignment="Top" Width="135" SelectionChanged="imageListBox_SelectionChanged" Visibility="Collapsed"/>
        <Image Height="234" HorizontalAlignment="Left" Margin="164,54,0,0" Name="image" Stretch="Fill" VerticalAlignment="Top" Width="224" />
</Grid>

接下来看一下WebClient类的一些成员:

image

这里用到OpenReadAsync方法以流的方式异步下载zip文件, 并在OpenReadCompleted事件中即下载完成后获得这个流。代码如下所示:

        private StreamResourceInfo imageResources;

        private void downloadButton_Click(object sender, RoutedEventArgs e)
        {
            WebClient wc = new WebClient();

            wc.OpenReadCompleted += new OpenReadCompletedEventHandler(wc_OpenReadCompleted);
            wc.OpenReadAsync(new Uri("/Resource/images.zip", UriKind.Relative));
        }

        void wc_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                imageResources = new StreamResourceInfo(e.Result, null);
                this.imageListBox.Visibility = System.Windows.Visibility.Visible;
            }
            else
            {
                throw e.Error;
            }
        }

在OpenReadCompleted事件若下载没有发生异常,ListBox被设置为可见并用System.Windows.Resources.StreamResourceInfo类对象来保存下载的zip流。

需要注意的是Uri路径的问题, 具有前导斜杠的相对 URI 表示相对于应用程序根的位置:

image

最后看一下取得图片并显示的代码:

        private void imageListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            BitmapImage bitmapImageSource = new BitmapImage();

            StreamResourceInfo imageResourceInfo = Application.GetResourceStream(imageResources, new Uri(this.imageListBox.SelectedItem.ToString(), UriKind.Relative));
            bitmapImageSource.SetSource(imageResourceInfo.Stream);

            this.image.Source = bitmapImageSource;
        }

这里使用Application.GetResourceStream去获得一个特定图片, 图片的相对路径是相对于zip文件的图片路径。

完整的后台代码如下:

    public partial class WebClientDemo : UserControl
    {
        private StreamResourceInfo imageResources;

        public WebClientDemo()
        {
            InitializeComponent();

            List<string> imageList = new List<string> { "01.jpg", "02.jpg", "03.jpg" };
            imageListBox.ItemsSource = imageList;
        }

        private void downloadButton_Click(object sender, RoutedEventArgs e)
        {
            WebClient wc = new WebClient();

            wc.OpenReadCompleted += new OpenReadCompletedEventHandler(wc_OpenReadCompleted);
            wc.OpenReadAsync(new Uri("/Resource/images.zip", UriKind.Relative));
        }

        void wc_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                imageResources = new StreamResourceInfo(e.Result, null);
                this.imageListBox.Visibility = System.Windows.Visibility.Visible;
            }
            else
            {
                throw e.Error;
            }
        }

        private void imageListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            BitmapImage bitmapImageSource = new BitmapImage();

            StreamResourceInfo imageResourceInfo = Application.GetResourceStream(imageResources, new Uri(this.imageListBox.SelectedItem.ToString(), UriKind.Relative));
            bitmapImageSource.SetSource(imageResourceInfo.Stream);

            this.image.Source = bitmapImageSource;
        }
    }

参考书籍: Introducing Silverlight 4, MSDN