用户操作
[留言]  [发消息]  [加为好友] 
订阅我的博客
XML聚合    FeedSky
订阅到鲜果
订阅到Google
订阅到抓虾
net_lover的公告
<div style="padding:4px;border:solid 1px #369;line-height:150%;margin:10px;"> 欢迎光临!您在阅读的过程中有任何建议或者意见,请发邮件或者留言,合作愉快! </div> <!--2--> <div align="center" style="width:160px;text-align:center;margin:auto;"> <script type="text/javascript"><!-- google_ad_client = "pub-8905127525253818"; /* 160x600, 创建于 09-3-24 */ google_ad_slot = "3547143261"; google_ad_width = 160; google_ad_height = 600; //--> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script> </div>
文章分类
【孟子E章】站点
【孟子E章】站点(RSS)
【孟子E章】网摘(RSS)
【孟子E章】网站(RSS)
【网上邻居】
.Net开发资源中心
【兔子】专栏
AppleVB 主页
Estyle(靳田)之狂想手扎
huahaoyueyuan
JavaProgramers的专栏
意玺的BLOG
最爱白菜
枕善居VB.NET源码博客
美丽眼睛看世界
酷狗的窝
阿赖
存档

原创  Silverlight 读取嵌入在.xap文件中的文件内容 收藏

假如在 SilverlightApplication6 工程中添加一个文件夹 Content ,下面放置一个 mxh.txt 文件和 mxh.jpg 的照片,文件内容随便写。在“解决方案浏览器”的文件属性中,设置“Build Action”为“Content”;“Copy to Output Directory”属性设置为“Do not copy”。
在 xaml 文件中输入: 

XAML 代码
<UserControl x:Class="SilverlightApplication6.MainPage"
    xmlns
="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x
="http://schemas.microsoft.com/winfx/2006/xaml"
    Width
="800" Height="600">
    
<Grid x:Name="LayoutRoot" Background="White">
    
<Canvas Width="800" Height="600">
      
<TextBox x:Name="TextBoxName" Height="30" Canvas.Top="10"></TextBox>
      
<Image x:Name="ImageNameIncude" Canvas.Top="60" Height="200"></Image>
      
<Image x:Name="ImageNameEmbed" Canvas.Top="260" Height="100"></Image>
    
</Canvas>
  
</Grid>
</UserControl>

  xaml.cs 内容输入: 

C# 代码
using System;
using System.Windows;
using System.Windows.Media.Imaging;
using System.Windows.Controls;
using System.IO;
using System.Windows.Resources;

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

      
// 读取文字
      StreamResourceInfo r = Application.GetResourceStream(new Uri("Content/mxh.txt", UriKind.Relative));
      StreamReader sr
= new StreamReader(r.Stream);
      TextBoxName.Text
= sr.ReadToEnd();
      sr.Dispose();

      
//显示 Build Action 为 Content  图片
      r = Application.GetResourceStream(new Uri("Content/mxh.jpg", UriKind.Relative));
      BitmapImage bmp1
= new BitmapImage();
      bmp1.SetSource(r.Stream);
      ImageNameIncude.Source
= bmp1;

      
//显示 Build Action 为 Resource  图片
      r = Application.GetResourceStream(new Uri("SilverlightApplication6;component/Content/mxh2.jpg", UriKind.Relative));
      BitmapImage bmp2
= new BitmapImage();
      bmp2.SetSource(r.Stream);
      ImageNameEmbed.Source
= bmp2;
    }
  }
}

 按F5进行编译预览,即可在  TextBox 中看到 mxh.txt文件的内容和显示孟宪会的照片。

 注意:分隔符“;component/”是必须的。

发表于 @ 2009年05月06日 17:19:00 | 评论( loading... ) | 编辑| 举报| 收藏

旧一篇:C# MVP Tim Dawson 发布 Silverlight Ribbon 控件 SandRibbon | 新一篇:Visual Studio 2010 初体验

  • 发表评论
  • 评论内容:
  •  
Copyright © net_lover
Powered by CSDN Blog