StreamResourceInfo类

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

下面一个实例讲解:

r_7777.jpg

代码如下:

 

ContractedBlock.gif ExpandedBlockStart.gif 代码
 
   
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;
}

}
}

 源代码下载

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值