Using MergedDictionaries in generic.xaml in Silverlight 3
Asked Answered
N

3

9

In WPF it was possible to organise the XAML for multiple user controls by keeping the markup in separate XAML files in the themes folder and then using MergedDictionaries to import them into generic.xaml:

<ResourceDictionary>
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="MyFirstControl.xaml" />
        <ResourceDictionary Source="MySecondControl.xaml" />
    </ResourceDictionary.MergedDictionaries>
</ResourceDictionary>

With the availability of the Silverlight 3 beta introducing merged dictionary support, it seemed like it might be possible to do the same with Silverlight user controls. But despite trying all combinations of build action on the merged dictionary files and the corresponding syntax for the source reference in generic.xaml, I can't seem to get it working.

Has anyone else tried? Does anyone know if it is possible and if so what I am doing wrong?


OK - so after numerous test projects, getting working samples in WPF and moving the XAML and C# code over to Silverlight 3 and it still failing, I did a full uninstall and reinstall of ALL the Silverlight 2 bits AND ALL the Silverlight 3 beta bits and finally got things working.

I can only assume that I somehow ended up with a faulty install of the beta - I don't know but it seemed like I was still running in the Silverlight 2 runtime despite apparently having the version 3 runtime installed.

Thanks Jared for taking a look at things and checking with the SL3 team...and thanks to Amy Dullard and Shawn Wildermuth for producing the instructions and batch files for running Silverlight 2 & 3 on the same machine.

Neoprene answered 27/3, 2009 at 9:21 Comment(0)
P
12

I just tried the following in a user control and it worked:

<UserControl.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="ResourcesA.xaml" />
            <ResourceDictionary Source="ResourcesB.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</UserControl.Resources>
<StackPanel>
    <Rectangle Width="100" Height="100" Fill="{StaticResource ResAColor}" />
    <Rectangle Width="100" Height="100" Fill="{StaticResource ResBColor}" />
</StackPanel>

But you specifically mention generic.xaml. What sort of problem are you having?

-- EDIT

Based on the additional comments, I talked with the SL3 team and got the following answer:

Works for me, using generic.xaml compiled as a Resource, and using the full resource syntax. There is a bug on not being able to use relative URIs for the Source in generic.xaml (31783) but the non-relative form should work just fine

<ResourceDictionary Source='/SilverlightClassLibrary1;component/CustomControl.xaml'/>

in generic.xaml, and modify the build actions for both generic.xaml and CustomControl.xaml to be Resource. Let me know if there’s still trouble—if you get a repro, I can take a look at it.

Does that help?

Photosynthesis answered 27/3, 2009 at 15:49 Comment(5)
I was under the impression that MergedDictionaries was not supported in Silverlight. Is this new in SL3?Agrapha
Yes - I have no problem using merged resources generally - but trying to use separate xaml files for the styles/templates of different usercontrols and bring them together as merged dictionaries in a generic.xaml file is what is giving me problems.Neoprene
Well, that's exactly what I am doing. But it is just not working for me. The code is a migration of a SL 2 project with existing controls in generic.xaml. I'll have to do some experimenting with sample projects to see if I can isolate the problem, but at least I know I'm on the right track. Thanks.Neoprene
I've got an assembly named "X" and a "Themes" folder in it, "Themes" contains generic.xaml + a myControl.xaml. How should generic xaml look with the merged dictionary bit ? I tried to follow what you've written above but I've failedSclerophyll
NOTE: I just found that using the full resource syntax also solves the same problem in WPF 4.5Northington
S
1

If MySecondControl.xaml uses a resource from MyFirstControl.xaml the order you add them to generic.xaml's ResourceDictionary won't matter. You'll need to redundantly include MyFirstControl.xaml in MySecondControl.xaml. MySecondControl.xaml should contain:

<ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source='/MyControls;component/Themes/MyFirstControl.xaml'/>
</ResourceDictionary.MergedDictionaries>
<!-- ... Contents of MySecondControl.xaml -->
Shoeblack answered 14/4, 2011 at 20:41 Comment(0)
D
0

I just worked through this issue. ResourceDictionaries do support MergedDictionaries, but for custom templated controls using Generic.xaml, Generic.xaml does not support MergedDictionaries. So there are two choices: (1) you either pile all your templates into Generic.xaml; or (2) you create YourOwnDictionary.xaml, merged all of your separate dictionaries into YourOwnDictionary.xaml, and reference YourOwnDictionary.xaml from usercontrols and pages. This appears to be a feature/bug from earlier Silverlight versions not supporting merged dictionaries.

Dressing answered 9/1, 2012 at 1:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.