Brian Genisio's House of Bilz

  Home  |   Contact  |   Syndication    |   Login
  72 Posts | 0 Stories | 186 Comments | 0 Trackbacks

News

Locations of visitors to this page

Archives

Post Categories

Who am I?

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>

<StackPanel x:Name="LayoutRoot" Orientation="Horizontal">
    <TextBox />
    <Button Content="{Binding Path=SearchButton, Source={StaticResource i18n}}" />
</StackPanel>

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>

<StackPanel x:Name="LayoutRoot" Orientation="Horizontal">
    <TextBox />
    <Button Content="{Binding Path=Strings.SearchButton, Source={StaticResource i18n}}" />
</StackPanel>
  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati
posted on Sunday, March 15, 2009 2:15 AM

Feedback

# re: Binding to Resources in Silverlight/WPF 4/18/2009 12:53 PM Sree
Thanks a lot man...
I set it to Public and breaking my head on why it is showing this error

# re: Binding to Resources in Silverlight/WPF 4/22/2009 11:14 AM David
Can you provide full source code project for download? It will be very helpful.

# re: Binding to Resources in Silverlight/WPF 4/24/2009 12:02 PM slyi
Excellent i did try this in SL2 beta 2 but it was not working at the time. Thanks for the useful pointer

# re: Binding to Resources in Silverlight/WPF 7/22/2009 7:46 PM slyi
Work well when add iNotifyProperyChanged to allow dynamicly changing language strings eg: http://wpf-e.spaces.live.com/blog/cns!2B248D261D0E0035!843.entry

# re: Binding to Resources in Silverlight/WPF 9/25/2009 8:23 AM Ken Smith
By the way, does anybody besides me think that the error messages that Visual Studio returns for these sorts of XAML errors are insane? How are we supposed to troubleshoot a problem when the only error we get is AG_E_PARSER_UNKNOWN_TYPE? Sheesh...

http://blog.wouldbetheologian.com/2009/09/and-three-more-silverlight-complaints.html

# re: Binding to Resources in Silverlight/WPF 3/10/2010 11:20 PM Vadim
Thanks a lot!!! it's help me!

Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification: