In WPF, this used to work fine:
<Page.Resources>
<ResourceDictionary Source="resources/Styles.xaml" />
</Page.Resources>
but adding a converter (see below) causes an error on the 2nd resource (Style.xaml
): Each dictionary entry must have an associated key
.
<Page.Resources>
<local:MySizeConverter x:Key="sizeConverter"/>
<ResourceDictionary Source="resources/Styles.xaml" />
</Page.Resources>
However, adding a key to the 2nd line (e.g. <ResourceDictionary x:Key="myStyleDict" Source="resources/Styles.xaml" />
causes the following error in code behind
The name 'aTextBlockUsedToWork' does not exist in the current context
where aTextBlockUsedToWork
could be successfully accessed in code behind before adding the key. Note that the converter works fine if I comment out the style resource. How can I have both of the resources working?
Tip You can create a resource dictionary file in Microsoft Visual Studio by using the Add > New Item… > Resource Dictionary option from the Project menu. Here, you define a resource dictionary in a separate XAML file called Dictionary1.
To add a Resource Dictionary into your WPF application, right click the WPF project > add a Resource Dictionary. Now apply the resource "myAnotherBackgroundColor" to button background and observe the changes.
Static Resources retrieved once by referencing element and used for the lifetime of the resources. Whereas, DynamicResources retrieve every time they are used. The downside of Dynamic resources is that they tend to decrease application performance.
In Extensible Application Markup Language (XAML), the ResourceDictionary class is typically an implicit collection element that is the object element value of several Resources properties, when given in property element syntax. For details on implicit collections in XAML, see XAML Syntax Terminology.
You need to use MergedDictionaries
to import another dictionary file, like this:
<Page.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="resources/Styles.xaml" />
</ResourceDictionary.MergedDictionaries>
<local:MySizeConverter x:Key="sizeConverter"/>
</ResourceDictionary>
</Page.Resources>
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