StreamResourceInfo类

                可以使用 StreamResourceInfo 处理碰巧为包(XAP 或 ZIP 文件)的流。如果已经返回作为 WebClient 请求结果的异步流并且该返回流确实是一个包含多个部件的包,则这个类很有用。若要获取这些部件,必须在 URI(在 GetResourceStream 调用中指定)中请求每个部件,同时在 zipPackageStreamResourceInfo 参数中将初始包指定为 StreamResourceInfo

下面一个实例讲解:

代码如下:

 

代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
//添加命名空间
using System.Windows.Media.Imaging; // BitmapImage
using System.Windows.Resources; // StreamResourceInfo

namespace StreamResourceInfoDemo
{
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();

//在该解决方案中添加的另一个应用程序项目
//注意点:在加载的资源前加上自己的项目的名称 /StreamResourceInfoDemo;component/
Image img1 = LoadImage(
"/SilverlightApplication;component/EmbeddedInApplicationAssembly.jpg");
this.stackPanel.Children.Add(img1);

// 设为起始页的Silverlight应用程序包中jpg文件资源
//注意点:即使设为启动项目的应用程序包也要在 /StreamResourceInfoDemo;component/
Image img2 = LoadImage("/StreamResourceInfoDemo;component/IncludedInApplicationPackage.jpg");
this.stackPanel.Children.Add(img2);

//在解决方案中添加的Silverlight类库项目
//注意点:(1)在加载资源前面加上类库名称
// (2)在被设为起始页的应用程序中添加对该程序集的引用
Image img3 = LoadImage(
"/SilverlightClassLibrary;component/EmbeddedInLibraryAssembly.jpg");
this.stackPanel.Children.Add(img3);
}

public Image LoadImage(string relativeUriString)
{
// Get the image stream at the specified URI that
// is relative to the application package root.
Uri uri = new Uri(relativeUriString, UriKind.Relative);
StreamResourceInfo sri
= Application.GetResourceStream(uri);

// Convert the stream to an Image object.
BitmapImage bi = new BitmapImage();
bi.SetSource(sri.Stream);
Image img
= new Image();
img.Source
= bi;
return img;
}

}
}

 源代码下载

posted @ 2011-01-28 09:56  焦涛  阅读(952)  评论(0)    收藏  举报