15.02.2015 Views

C# 4 and .NET 4

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

594 ❘ ChaPTer 22 lOcAlizAtiOn<br />

FneT resources with WPf<br />

You can add .<strong>NET</strong> resources to a WPF application similar to the way you do with other applications.<br />

Define the resources named Button1Text <strong>and</strong> Button1Message in the file Resources.resx. By default this<br />

resource file has an Internal access modifier to create the Resources class. To use it from within XAML,<br />

you need to change this to Public within the Managed Resources Editor.<br />

To use the generated resource class, you need to change the XAML code. Add an XML namespace<br />

alias to reference the .<strong>NET</strong> namespace Wrox.ProCSharp.Localization.Properties as shown. Here, the<br />

alias is set to the value props. From XAML elements, properties of this class can be used with the<br />

x:Static markup extension. The Content property of the Button is set to the Button1Text property of<br />

the Resources class.<br />

<br />

<br />

<br />

<br />

<br />

code snippet WPFApplicationUsingResources/MainWindow.xaml<br />

To use the .<strong>NET</strong> resource from code-behind, you can just access the Button1Message property directly in<br />

the same way you did with Windows Forms applications:<br />

private void Button_Click(object sender, RoutedEventArgs e)<br />

{<br />

MessageBox.Show(Properties.Resources.Button1Message);<br />

}<br />

Now the resources can be localized in the same way as you’ve done before.<br />

The advantages of using .<strong>NET</strong> resources for localization of WPF applications are:<br />

➤<br />

➤<br />

.<strong>NET</strong> resources can be easily managed.<br />

x:Static bindings are checked by the compiler.<br />

Of course, there are also disadvantages:<br />

➤<br />

➤<br />

➤<br />

You need to add the x:Static bindings to the XAML file, <strong>and</strong> there’s no designer support for this.<br />

Binding is done to the generated resource classes that use the ResourceManager. You<br />

would need to do some additional plumbing to support other resource managers, such as the<br />

DatabaseResourceManager, which is discussed later in this chapter.<br />

There’s no type-converter support that can be used with other XAML elements.<br />

Xaml resource dictionaries<br />

Instead of using .<strong>NET</strong> resources for localization of WPF applications, you can work directly with XAML to<br />

create localized content. This has its own advantages <strong>and</strong> disadvantages. The steps for a localization process<br />

can be described by these actions:<br />

➤<br />

➤<br />

➤<br />

➤<br />

Create a satellite assembly from the main content.<br />

Use resource dictionaries for localizable content.<br />

Add x:Uid attributes to elements that should be localized.<br />

Extract localization content from an assembly.<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!