1
votes

I'm at my ropes end with this. I have spent countless hours trying to figure this out and no such luck.

Short Explanation of Problem

Inside my custom control class, when I check Application.Current.Resources["key"] I am returned null. This "Key" style is inside a local dictionary which is supposed to be merged with the Application.Current.Resources by my Control Library's themes/generic.xaml resource.

How do I reference/confirm a reference to a MergedDictionary in my SilverLight Control Library's themes/generic.xaml.

Is this even possible or is my thinking on how merged resources are suppose to be merged entirely wrong?

Please help. Thanks in advance.

Long Explanation of Problem

I have a Silverlight Control Library with a Controls folder, and a Themes folder. Inside the Themes folder I have generic.xaml. Its content:

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="/SilverLightLib;component/Themes/EnhancedLabelDict.xaml" />
    </ResourceDictionary.MergedDictionaries>
</ResourceDictionary>

Inside the Themes folder I have EnhancedLabelDict.xaml. Its content:

<Style x:Key="ReadOnlyTextBox" TargetType="TextBox">
        <Setter Property="BorderThickness" Value="1"/>
        <Setter Property="Background" Value="#FFFFFFFF"/>
        <Setter Property="Foreground" Value="#FF000000"/>
        <Setter Property="Padding" Value="2"/>
        <Setter Property="BorderBrush">
        <!-- A lot more code -->
</Style>

Both these files build action is set to Page.

Now I have no idea if the generic.xaml is even loading my resource. The only way I can tell is if I put some un-formatted text between . This causes an error.

If I use an incorrect path to my ResourceDictionary, I receive a run time error - 'Failed to assign to property 'System.Windows.ResourceDictionary.Source'

Inside my Controls folder, I have EnhancedLabel.cs which extends ContentControl. Inside it's constructor, I create a new TextBox and assign it's style like so:

Style style = Application.Current.Resources["ReadOnlyTextBox"] as Style;
this.textBox.Style = style;

I have this style in both the App.xaml and my EnhancedLabelDict.xaml which is inside my library. When I comment out the Style in App.xaml, the 'ReadOnlyTextBox' style is not found (null). Uncomment it, it is found.

I don't understand why I cannot reference my style from within my EnhancedLabel.cs.

If I take the EnhancedLabelDict.xaml, add it to a Themes folder inside a Resources folder inside my main Application. If I then add the following to my App.xaml:

<ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="/SilverLightPOC;component/Resources/Themes/EnhancedLabelDict.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>

My control works! So other than the path, there is nothing different. But this doesn't work because I don't want to have to store Dictionary files that my Library depends on, inside the main application.

Please help.

1
Personally I don't use generic.xaml for anything. Declare your MergedDictionaries (with correct path) in your app.xaml of the same project you're trying to reference from and in your case just point your app.xaml to your EnhancedLabelDict.xaml and that should be the end of it. Submitted as comment because I wouldn't be able to know if it's a solution unless I could check it against yours.Chris W.
@ChrisW. Thanks for that suggestion. I was hoping to have the Dictionary that EnhancedLabel relies on and references in the same library as the control. I did not want to have to merge the dictionary inside the main app's App.xaml. I guess that would make it less portable. Again thanks for your suggestion and I'll use that for now but I will continue to look for another solution.Agilis
Wait, what? I think you may misunderstand. App.xaml only points to your resource dictionary. You don't actually make your app.xaml the resource dictionary? I have a solution with 100+ projects in it that uses 3 resource dictionaries that live in only 1 currently and it works dandy ;)Chris W.
@ChrisW. Well currently, I am using my App.xaml to host the different styles I'm using throughout the application. It is also now referencing a Resource dictionary stored in my Silver Light Control library (Separate project) through MergedDictionaries. I will later split my styles in App.xaml into many different Resource dictionaries, like I do with CSS Stylesheets.Agilis
Ya, that's what we do. All the resource dicts sit in one project and then each project refers to them via app.xaml, works out quite well, especially when setting globally inherited templates when establishing a theme. Should I be submitting this stuff as an actual answer? lolChris W.

1 Answers

0
votes

There is an optimization bug in Silverlight when you have more than 3 levels deep of nested dictionaries - they're not loaded unless you use a workarround.

See Adding a Merged Dictionary to a Merged Dictionary