,

Localizing Images Using Resx Files

The Resx Designer in Visual Studio 2012 has been improved and now provides better support for image files.

An image file can be added to a resx file by opening the resx file in design view and by selecting the Images item in the Resx Designer. You can then drag and drop an image file onto the Resx Designer surface.

An Image control can now be bound to a localized image, as shown in the following excerpt:

<Image Source="{Binding Resources.Instance.ImageResourceName,
       Source={StaticResource BindableResources},
       Converter={StaticResource BitmapImageConverter}}" />

As in the previous string resources example, the data-binding’s Source property uses the ResourceBindingWrapper BindableResources instance defined in the app’s resources. The data-binding’s Path property is set to Resources.Instance.ImageResourceName, which is a byte[] property for the image.

Notice the use of the BitmapImageConverter in the previous excerpt. This IValueConverter is responsible for taking the image data in the form of a byte[] and converting it into an image (see Listing 19.3).

LISTING 19.3. BitmapImageConverter Class


public class BitmapImageConverter : IValueConverter
{
    public object Convert(object value, Type targetType,
        object parameter, System.Globalization.CultureInfo culture)
    {
        BitmapImage bitmapImage = new BitmapImage();
       using (MemoryStream stream = new MemoryStream(bytes))
       {
              stream.Seek(0, SeekOrigin.Begin);
              bitmapImage.SetSource(stream);
       }
        return bitmapImage;
    }

    public object ConvertBack(object value, Type targetType,
        object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.143.31