I know and use two methods to store and access resources on application scope:
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
Static Resource - Static resources are the resources which you cannot manipulate at runtime. The static resources are evaluated only once by the element which refers them during the loading of XAML.
A resource is an object that can be reused in different places in your application. WPF supports different types of resources. These resources are primarily two types of resources: XAML resources and resource data files. Examples of XAML resources include brushes and styles.
A resource is an object that can be reused in different places in your app. Examples of resources include brushes and styles. This overview describes how to use resources in Extensible Application Markup Language (XAML). You can also create and access resources by using code.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With