Binding to Resources in Silverlight/WPF

Binding to Resources in Silverlight/WPF

Shout it kick it on DotNetKicks.com

This article is describing how to solve the problem in SIlverlight, but the problem can be solved in WPF using the exact same technique.

So, now it is time to internationalize our application.  I want to use the built-in "resx" mechanism for i18n in .net, so I create myself a resource file and start adding strings to the table:

image

Visual Studio automatically generates the resource class for me, and I am ready to bind to the data in my XAML:

<UserControl.Resources> <l:i18n x:Key="i18n" /> </UserControl.Resources>

But I run my application and I get this exception: Unhandled Error in Silverlight 2 Application AG_E_PARSER_BAD_TYPE.  I get this error message because the resx file is set to create my resource class as an "internal" class.  Essentially, the SIlverlight engine doesn't have permission to instantiate a new i18n object, and it dies.

Fortunately, in Visual Studio 2008, they have given us a way to solve this problem.  In the Resx editor, you can change the protection of the class it generates from internal to public.  So, I do exactly that:

image

Now my XAML should be able to bind to the resource, right?  Wrong: Unhandled Error in Silverlight 2 Application AG_E_PARSER_UNKNOWN_TYPE.  This time, it is complaining because the constructor to this public class that Visual Studio generated is internal!  The class is public, but the constructor is internal, so the Silverlight/WPF engine still can't instantiate the object.

I searched the interwebs, and I'm not alone with this problem.  Most people solve the problem by adding a custom build step that modifies the code.  Some even suggest changing the constructor to public by hand every time you update the resource table.  I am here to say: THERE IS A BETTER WAY!  I just created a public class with a single property that exposes the class:

public class i18nPublic { private readonly static i18n resources = new i18n(); public i18n Strings { get { return resources; } } }

By adding this class, I can make a slight modification to the resource type and the binding path and I am now binding directly to the resources.

<UserControl.Resources> <l:i18nPublic x:Key="i18n" /> </UserControl.Resources>

This article is part of the GWB Archives. Original Author: Brian Genisio's House Of Bilz

New on Geeks with Blogs

  • We Won The One Award I Actually Care About

    Full Scale made the Inc. 5000 for the fifth year straight, the 12th listing across my three companies. Here is why the one award you cannot buy is worth stopping for.

  • Your Customers Build the Features Now

    I let a tool I liked sit dead for a year rather than build the features I wanted. An MCP server meant I never had to, and your customers can do the same to your product.

  • Get the Size of a Directory in Linux the Easy Way

    du -sh for the quick answer, ncdu for the cleanup, df for the disk itself: every command for checking directory size in Linux, plus why du and df never agree.

  • Vim Search and Replace: The Ultimate Guide

    One :%s command replaces every match in a file before a find dialog would even open. The Vim substitute patterns worth the muscle memory: flags, ranges, capture groups, and multi-file edits.