WPF整理-二进制资源和内容
WPF中的Binary Resource(二进制资源)是相对于前面所说的Logical resource(逻辑资源)而说的,一般指Image、XML文件等。
注意:这里说的是Resource"资源",和Content"内容"是不同的。
1.Content VS Resource
一般我们向工程中添加一个二进制的资源,如图片。我们设置它的属性,设置成资源和内容是不同的!

简单的说下两者的区别:
“When the Build Action is set to Content (as in the jellyfish example), the resource is not
included in the assembly. This makes it more appropriate when the resource needs to change
often (perhaps by a designer) and a rebuild would be undesirable. Also, if the resource is
large, and not always needed, it's better to leave it off to the resulting assembly. Note that to
access the resource, the exact same syntax is used. This is possible because WPF adds the
AssemblyAssociatedContentFile attribute to the assembly, specifying the name of the
resource file. Here's a view with .NET Reflector:”

“That's why we were able to replace the jellyfish image with a desert image and get it to show
correctly given the name jellyfish.jpg without doing any kind of rebuilding.”
WHILE
“When the Build Action is set to Resource, the image file is stored as a resource inside the compiled
assembly. This is because the Build Action was set to Resource on the image. This makes
the actual image file unnecessary when deploying the application.
These resources are part of the assembly and are stored in a resource named
MyApplication.g.resources, where MyApplication is the name of the
assembly. Here's a snapshot from .NET Reflector:”

2.Access
对于Image,无论设置成Content,还是Resource,都可以很方便的访问,如下:
xaml中
<Image Source="Images/5.png" Width="100" Height="100" />
<Image Source="Images/6.png" Width="100" Height="100" />
C#中
image1.Source = new BitmapImage(new Uri("Images/5.png", UriKind.Relative));
image2.Source = new BitmapImage(new Uri("Images/6.png", UriKind.Relative));
并没有区别。
3.Point of Interest
其他的,如xml file,两者访问方法就有区别了!
“Accessing a binary resource in XAML is pretty straightforward, but this works for standard
resources such as images. Other types of resources may be used in code, and this requires
a different approach.”
譬如有这样的一个xml file

如果,我们将生产操作设置成为Content,则我们可以这样访问:
var books = XElement.Load("Xml/books.xml");
var bookList = from book in books.Elements("Book")
orderby (string)book.Attribute("Author")
select new
{
Name=(string)book.Attribute("Name"),
Author=(string)book.Attribute("Author")
};
foreach (var book in bookList)
{
txtBook.Text += book.ToString() + Environment.NewLine;
}
或者是:
var info = Application.GetContentStream(new Uri("Xml/books.xml", UriKind.Relative));
var books = XElement.Load(info.Stream);
var bookList = from book in books.Elements("Book")
orderby (string)book.Attribute("Author")
select new
{
Name=(string)book.Attribute("Name"),
Author=(string)book.Attribute("Author")
};
foreach (var book in bookList)
{
txtBook.Text += book.ToString() + Environment.NewLine;
}
如果设置成Resource,则以上程序会报告无法找到xml file。

因此,设置成Resource时,我们应该这样访问:
var info = Application.GetResourceStream(new Uri("Xml/books.xml", UriKind.Relative));
var books = XElement.Load(info.Stream);
var bookList = from book in books.Elements("Book")
orderby (string)book.Attribute("Author")
select new
{
Name=(string)book.Attribute("Name"),
Author=(string)book.Attribute("Author")
};
foreach (var book in bookList)
{
txtBook.Text += book.ToString() + Environment.NewLine;
}
两种情况下,程序运行如下:
WPF整理-二进制资源和内容的更多相关文章
- wpf之二进制资源
一.当需要添加图片.音频.视屏的资源到wpf项目里是,可以直接把文件添加到项目里 右击add->existing item. 1.如果想将外部文件编异常目标成为二进制资源,在文件的属性窗口 Bu ...
- WPF 应用程序资源、内容和数据文件
MSDN相关介绍: http://msdn.microsoft.com/zh-cn/library/aa970494(v=vs.100).aspx 内容文件(Content Files)内容文件简单的 ...
- WPF整理-Style
"Consistency in a user interface is an important trait; there are many facets of consistency, ...
- 《Programming WPF》翻译 第6章 3.二进制资源
原文:<Programming WPF>翻译 第6章 3.二进制资源 尽管ResourceDictionary和系统级别的资源适合于作为数据存在于对象中,然而,并不是所有的资源都能很好的满 ...
- WPF整理-使用逻辑资源
"Traditional application resources consist of binary chunks of data, typically representing thi ...
- WPF 资源(StaticResource 静态资源、DynamicResource 动态资源、添加二进制资源、绑定资源树)
原文:WPF 资源(StaticResource 静态资源.DynamicResource 动态资源.添加二进制资源.绑定资源树) 一.WPF对象级(Window对象)资源的定义与查找 实例一: St ...
- WPF中的资源(二) - 二进制资源
原文:WPF中的资源(二) - 二进制资源 WPF中的二进制资源,就是类似于MFC中在对话框程序中添加的图片.字符串等资源,程序在运行时将其转换成二进制,以供程序使用. 下面以将字符串转换成二进制为例 ...
- WPF整理-XAML构建后台类对象
1.XAML 接触WPF的第一眼就是XAML---XAML是用来描绘界面的.其实不然! "Actually, XAML has nothing to do with UI. It's mer ...
- WPF学习之资源-Resources
WPF学习之资源-Resources WPF通过资源来保存一些可以被重复利用的样式,对象定义以及一些传统的资源如二进制数据,图片等等,而在其支持上也更能体现出这些资源定义的优越性.比如通过Resour ...
随机推荐
- git 安装教程
昆,简单说下安装教程1,安装Git2,安装TortoiseGit3,打开第一步安装的git工具GIT BASH
- Python Virtualenv运行Django环境配置
系统: RHEL6.5 版本说明: Python-3.5.0 Django-1.10.4 virtualenv:为每个项目建立不同的/独立的Python环境,你将为每个项目安装所有需要的软件包到它们各 ...
- 编译器--__attribute__ ((packed))
1. __attribute__ ((packed)) 的作用就是告诉编译器取消结构在编译过程中的优化对齐,按照实际占用字节数进行对齐,是GCC特有的语法.这个功能是跟操作系统没关系,跟编译器有关,g ...
- ASP.NET知识总结(9.使用Cookies实现购物车)
ListInfo.aspx向购物车的添加商品的方法 private void GouWu(string name, double price, string id) { //往购物车中添加商品 Htt ...
- 技术杂记-改造具有监控功能的数据库连接池阿里Druid,支持simple-jndi,kettle
kettle内置的jndi管理是simple-jndi,功能确实比较简单,我需要监控kettle性能,druid确实是很不错的选择,但没有提供对应的支持,我改进了druid源码,实现了simple-j ...
- Dark Mobile Bank之移动银行应用仿冒攻击威胁分析报告
一.背景 据“第十五次全国信息网络安全状况暨计算机和移动终端病毒疫情调查”调查结果显示,2015年移动终端的病毒感染比例为50.46%,相对于2014年增长了18.96%,移动终端病毒感染率涨幅较大, ...
- [转]ubuntu 下无法启动chrome
这很不爽,google了半天也不知道答案(搜索到要重装chrome,可是我怎么都卸载不干净.....),最终解决方法如下: -------------------------------------- ...
- 清理C盘系统垃圾文件-批处理方式
很多时候安装软件越来越多,部分软件产生的临时文件.垃圾文件常常存在于C盘系统盘中:日积夜累直接导致可用的系统盘空间越来越小,直到没有多余的空间为止, 最后可能的结果是系统异常.软件无法正常运行:此时可 ...
- Android应用请求获取Root权限
应用获取Root权限的原理:让应用的代码执行目录获取最高权限.在Linux中通过chmod 777 [代码执行目录] /** * 应用程序运行命令获取 Root权限,设备必须已破解(获得ROOT权限) ...
- IIS与Apache共用80端口
Windows server 2003服务器上安装有默认 IIS 6和Apache两个服务器,IIS运行的一个.net程序,apache运行php程序,现在想让它们同时都能通过80端口访问,设置起来还 ...