Geeks With Blogs

News Locations of visitors to this page
Brian Genisio's House of Bilz
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>
Posted on Sunday, March 15, 2009 2:15 AM .NET , Silverlight | Back to top


Comments on this post: Binding to Resources in Silverlight/WPF

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

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

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

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

# re: Binding to Resources in Silverlight/WPF
Requesting Gravatar...
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
Left by Ken Smith on Sep 25, 2009 8:23 AM

# re: Binding to Resources in Silverlight/WPF
Requesting Gravatar...
Thanks a lot!!! it's help me!
Left by Vadim on Mar 10, 2010 11:20 PM

Your comment:
 (will show your gravatar)


Copyright © Brian Genisio's House Of Bilz | Powered by: GeeksWithBlogs.net