WPF中的Binary Resource(二進制資源)是相對於前面所說的Logical resource(邏輯資源)而說的,通常指Image、XML文件等。app
注意:這裏說的是Resource"資源",和Content"內容"是不一樣的。ide
通常咱們向工程中添加一個二進制的資源,如圖片。咱們設置它的屬性,設置成資源和內容是不一樣的!ui
簡單的說下二者的區別:this
「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:」spa
「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.」 3d
WHILErest
「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:」code
對於Image,不管設置成Content,仍是Resource,均可以很方便的訪問,以下:xml
xaml中blog
<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));
並無區別。
其餘的,如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; }
兩種狀況下,程序運行以下: