15.01.2013 Views

Free-ebooks-library - Bahar Ali Khan

Free-ebooks-library - Bahar Ali Khan

Free-ebooks-library - Bahar Ali Khan

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

The resource designer adds images as typed Image objects<br />

(System.Drawing.dll), rather than as byte arrays, making them<br />

unsuitable for WPF applications.<br />

Creating a .resx file at the command line<br />

If you’re working at the command line, you must start with a .resx file that has a<br />

valid header. The easiest way to accomplish this is to create a simple .resx file programmatically.<br />

The System.Resources.ResXResourceWriter class (which, peculiarly,<br />

resides in the System.Windows.Forms.dll assembly) does exactly this job:<br />

using (ResXResourceWriter w = new ResXResourceWriter ("welcome.resx")) { }<br />

From here, you can either continue to use the ResXResourceWriter to add resources<br />

(by calling AddResource) or manually edit the .resx file that it wrote.<br />

The easiest way to deal with images is to treat the files as binary data and convert<br />

them to an image upon retrieval. This is also more versatile than encoding them as<br />

a typed Image object. You can include binary data within a .resx file in base 64 format<br />

as follows:<br />

<br />

Qk32BAAAAAAAAHYAAAAoAAAAMAMDAwACAgIAAAAD/AA....<br />

<br />

or as a reference to another file that is then read by resgen:<br />

<br />

flag.png;System.Byte[], mscorlib<br />

<br />

When you’re done, you must convert the .resx file by calling resgen. The following<br />

converts welcome.resx into welcome.resources:<br />

resgen welcome.resx<br />

The final step is to include the .resources file when compiling, as follows:<br />

csc /resources:welcome.resources MyApp.cs<br />

Reading .resources files<br />

If you create a .resx file in Visual Studio, a class of the same name<br />

is generated automatically with properties to retrieve each of its<br />

items.<br />

The ResourceManager class reads .resources files embedded within an assembly:<br />

ResourceManager r = new ResourceManager ("welcome",<br />

Assembly.GetExecutingAssembly());<br />

Resources and Satellite Assemblies | 667<br />

Assemblies

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!