Code generation for Resources in Silverlight (and how to get rid of the internal constructor)
Thursday, August 6, 2009
Localization and internationalization have never been great in Silverlight, but are totally possible and kind of easy with a bit of manual work.
There are a lot of articles out there focused on localization so I won’t dig deep in that. Instead, I want to make a note about the generated code for the resources and especially the nasty internal constructor.
By default, when you add a new resource, the generated code is internal. However, if you want to use the resources in XAML then you can go with Public access modifier:
OK, nothing special yet.
What you will notice is that you still can’t use the resources, because the constructor of the generated class is internal. All properties that are generated are static and in most cases you don’t need a constructor. But that’s not the case here. We want to use the resources in XAML so it has to create an instance of this class.
Of course a possible solution is to open the generated class, change the modifier from internal to public and that’s it. Well, yes, but you have to do this every time you add/update a resource in your file. Developers are lazy these days so I know you want something that can automate this process.
The resource class is automatically generated by a custom tool called PublicResXFileCodeGenerator (in case of public code generation). Guy Smith-Ferrier created another custom tool that behaves just like this one, except that it generates a public constructor. Read Guy’s post and download his tool to fix future problems with resources.
Entry Filed under: Localization,Silverlight. .
Trackback this post | Subscribe to the comments via RSS Feed