Code generation for Resources in Silverlight (and how to get rid of the internal constructor)

August 6th, 2009 § Leave a Comment

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:

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.

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

Gravatar
WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

What’s this?

You are currently reading Code generation for Resources in Silverlight (and how to get rid of the internal constructor) at Emil Stoychev.

meta

Follow

Get every new post delivered to your Inbox.