Style uses the Resourcewrapper class

Topics: Prism v4 - Silverlight 4
Jul 7, 2012 at 1:18 PM

Dear All,

I have a ResourceWrapper class that helps me localize my project, works fine so far.

I wanted to use that class as well in my styling (style.xaml) but I always get an error of the resource with key/name not found ...!!

I am not sure if this is related to MEF or PRISM, I just followed this MSDN article:

http://msdn.microsoft.com/en-us/library/dd882554(v=VS.95).aspx

Here is my declaration of the resourcewrapper in the app.xaml:

<Application.Resources>
		<ResourceDictionary>
			<ResourceDictionary.MergedDictionaries>
				<ResourceDictionary>
					<Infrastructure:ResourceWrapper x:Key="ResourceWrapper" />
				</ResourceDictionary>
				<ResourceDictionary Source="Assets/Styles/Styles.xaml"/>
				<ResourceDictionary>
					<IdeaInfra:NullVisibilityConverter x:Key="nullVisibilityConverter" />
					<IdeaInfra:BooleanToVisibilityConverter x:Key="booleanToVisibilityConverter"/>
					<IdeaInfra:MultiStringFormatConverter x:Key="MultiStringConverter"/>
					<IdeaInfra:SubstitutionConverter x:Key="substitutionConverter"/>
					<IdeaInfra:IntToBooleanConverter x:Key="intToBooleanConverter"/>
				</ResourceDictionary>
			</ResourceDictionary.MergedDictionaries>
		</ResourceDictionary>
	</Application.Resources>
</Application>

I also have a reference to "Infrastructue" in the xmlns.

and here how I access it from the style.xaml:

<Style x:Key="LinkStyle" TargetType="HyperlinkButton">
		<Setter Property="Background" Value="{StaticResource HighLightColorBrush}" />
		<Setter Property="BorderThickness" Value="1" />
		<Setter Property="BorderBrush" Value="#FF9D9492" />
		<Setter Property="Foreground" Value="{StaticResource NavigationForegroundColorBrush}" />
		<Setter Property="FontSize" Value="12" />
		<!--<Setter Property="FontFamily" Value="{Binding Path=ApplicationStrings.FontName, Source={StaticResource ResourceWrapper}}"/>-->
		<Setter Property="Cursor" Value="Hand" />
		<Setter Property="MinHeight" Value="28" />
</Style>

The commented line above is causing the error if uncommented !!.

Does anyone knows how to solve this issue...

Best regards

 

 

Developer
Jul 10, 2012 at 5:56 PM

Hi,

Based on what you are describing, it seems that the ResourceWrapper resource could not be located by the LinkStyle style. This could be related to where the app.xaml and the style.xaml files are located in your solution.

As a starting point you can check the following known issue in Prism, where several problems related to resources and modules are described, with possible workarounds:

If that is not the case, it would be helpful if you could provide us with more information about the structure of your project and the locations of the involved files inside it.

I hope you find this useful,

Damian Cherubini
http://blogs.southworks.net/dcherubini

Jul 13, 2012 at 7:09 PM

Hello DCherubini,

You are correct, the resourcewrapper class is not found !!

The Risk project contains the app.xaml which contains that last posted merged directory above. The Style.xaml aslo and The Resourcewrapper Class are within the same project as well.

The resource (.resx) files are all in a separate following project (Risk.Infra).

Have a look at this image that describes the structure layout more clearly.

https://skydrive.live.com/redir?resid=B7C7E0C3F0481483!120

Best regards

Developer
Jul 16, 2012 at 7:20 PM

Hi,

Based on my understanding, this behavior is caused by the way you are defining the resources in your application. As mentioned in this related thread, the search for a defined resource starts by crawling from the root of the ResouceDictionary and not from the first root, for example in your case the search will start from the ResourceDictionary containing the Style. Hence, in order to avoid this, for example you could try defining this resources at the same level.

Also, take into account that as this may not be strictly related to Prism, I believe you could find better support about this subject for example in the Silverlight forums.

Regards,

Agustin Adami
http://blogs.southworks.net/aadami

Jul 17, 2012 at 1:25 AM
Edited Jul 17, 2012 at 1:26 AM

Hello aadami,

You mean I should define the resource class in the same dictionary as the style.xaml.

I believe you understand that the resourcewrapper class is the issue here !!!

If so, how is this possible ... can you show me according to my posted script.

Best

Developer
Jul 17, 2012 at 12:40 PM
Edited Jul 17, 2012 at 12:40 PM

Hi,

Based on my understanding of your scenario, the ResourceDictionary where your style is defined (style.xaml) is the one that can't locate your defined ResourceWrapper, and I believe this is because of the aforementioned behavior.

For example as a quick test to check this, you could define your resources like this:

<Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary>
                    <Infrastructure:LocalizedStrings x:Key="ResourceWrapper"/>
                </ResourceDictionary>
            </ResourceDictionary.MergedDictionaries>
            <Style x:Key="LinkStyle" TargetType="HyperlinkButton">
                <Setter Property="FontFamily" Value="{Binding Path=ApplicationStrings.FontName, Source={StaticResource ResourceWrapper}}"/>
        (...)
            </Style>
        </ResourceDictionary>
    </Application.Resources>

This way as your Style will be defined in the same resource dictionary that contains the ResourceWrapper, I believe you should be able to consume it without problems.

Please let me know if I have misunderstood your scenario,

Regards,

Agustin Adami
http://blogs.southworks.net/aadami

Jul 18, 2012 at 11:02 AM

Hello Adami,

The code you posted require to add key to Resourcedictionary to compile:

	<Application.Resources>
		<ResourceDictionary>
			<ResourceDictionary.MergedDictionaries>
				<ResourceDictionary>
					<Infrastructure:ResourceWrapper x:Key="ResourceWrapper" />
				</ResourceDictionary>
			</ResourceDictionary.MergedDictionaries>
			<Style x:Key="LinkStyle" TargetType="HyperlinkButton">
				<Setter Property="FontFamily" Value="{Binding Path=ApplicationStrings.FontName, Source={StaticResource ResourceWrapper}}"/>
			</Style>

			<ResourceDictionary Source="Assets/Styles/Styles.xaml"  x:Key="wss"/>
			<ResourceDictionary x:Key="ws">
			(..... other resources .........)
			</ResourceDictionary>
		</ResourceDictionary>
	</Application.Resources>

But it gives another error "Set property '' threw an exception" on application InitializeComponent().
I have something to add if it would help figur out my problem.
If I add the complete template of the label control and do the font binding as shown here it works.
But this solution will not be Feasible as it requires to get every and each control template to do the binding (nightmare for me) !!
Best regards
Developer
Jul 18, 2012 at 7:05 PM

Hi,

Based on my understanding, the problem is that you cannot add a ResourceDictionary inside another ResourceDictionary; you have to merge them inside the MergeDictionaries tag. The code-snippet posted by Agustin Adami is not meant to add entire ResourceDictionaries but resources directly (that is, the contents of the ResourceDictionaries) and therefore, you will need to add all styles / templates directly in the Application ResourceDictionary.

However, as discussed previously, this is a limitation in Silverlight and is not related to Prism. Therefore, you might find better support for this in the Silverlight forums:

Regards,

Damian Cherubini
http://blogs.southworks.net/dcherubini