假如在 SilverlightApplication6 工程中添加一个文件夹 Content ,下面放置一个 mxh.txt 文件和 mxh.jpg
的照片,文件内容随便写。在“解决方案浏览器”的文件属性中,设置“Build Action”为“Content”;“Copy to Output
Directory”属性设置为“Do not copy”。
在 xaml 文件中输入:
XAML
代码
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--><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>
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#
代码
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->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;
}
}
}
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/”是必须的。另外注意程序集 SilverlightApplication 的名字。
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/15723462/viewspace-594344/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/15723462/viewspace-594344/
本文介绍如何在Silverlight应用程序中加载不同类型的资源文件,包括文本和图片,并展示了如何通过代码实现资源的读取与显示。

被折叠的 条评论
为什么被折叠?



