Julian
Julian

Reputation: 275

Best practice for using resources in a WPF project

I know and use two methods to store and access resources on application scope:

  1. Properties\Resources.resx
  2. create a folder and place for example images there, setting their build mode to Resource

What is the difference between the two methods in terms of performance and complexity, when should each be used and how are the resources best consumed in WPF and VB or C# code in each way?

Thanks in advance,

Julian

Upvotes: 9

Views: 3799

Answers (1)

Martin Liversage
Martin Liversage

Reputation: 106936

The "natural" way of referencing resources like images in a WPF project is your second option. You can use a relative URI to point to the image and WPF will lazy load it. You can reference resources in other assemblies using pack URI syntax.

Using Resources.resx will code-generate properties that loads resources when referenced. Resources can be strings, images, icons or a byte arrays. Using {x:Static} in XAML allows you to reference the static properties generated by the code-generator but often you will need a converter to convert the resource type into a type usable by WPF.

There is some support for localization using Resources.resx and if you want to provide a multi-lingual application you could store the translated strings in Resources.resx. However, WPF localization as described by Microsoft is not based on Resources.resx.

For images, the second option is much easier. For strings, the first option is probably easier but instead you could stay in XAML and create a ResourceDictionary.

Upvotes: 7

Related Questions