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;
            }

两种情况下,程序运行如下:

  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值