Binary Resources
Although ResourceDictionary
and
the resource scope system are fine for data that can easily be contained
in an object, not all resources fit comfortably into this model. Often
we need to deal with binary streams. For example, images, audio, and
video have efficient binary representations, but they are not
particularly at home in markup, and in the world of objects they are
usually represented by wrappers for the underlying data. Markup itself
also presents a challenge: XAML pages must somehow get built into our
applications. So a means of dealing with binary streams is
needed.
WPF does not introduce any new technology for dealing with binary data. The .NET Framework has always provided mechanisms for dealing with embedded binary streams, and WPF simply uses these.
The lowest level of stream support lets you embed resource streams
into any assembly. This is a simple matter of supplying the files you
would like to embed to the compiler. In Visual Studio, you do this by
setting a file's Build Action property to Embedded Resource. This copies
the contents of the file into the assembly as an embedded stream. The
stream can be retrieved at runtime using the Assembly
class's GetManifestResourceStream
method, as Example 12-26 shows.
Example 12-26. Retrieving assembly manifest resources
Assembly asm = Assembly.GetExecutingAssembly( ); Stream s = asm.GetManifestResourceStream("StreamName");
Streams embedded in this way are called assembly manifest resources. Although WPF ultimately ...
Get Programming WPF, 2nd Edition 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.