Accessing binary resources in code

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.

Getting ready

Make sure Visual Studio is up and running.

How to do it...

We'll create an application that shows book information read programmatically from an XML file stored as a resource:

  1. Create a new WPF Application named CH02.BinaryResourcesInCode.
  2. Add the books.xml (found in the downloadable source for this chapter) file as a resource (make sure Build Action is set to Resource). As an alternative, you can create the file yourself and type its contents as shown in the next step.
  3. The books.xml file looks something ...

Get Windows Presentation Foundation 4.5 Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.