I'm trying to figure out how to programmatically apply a theme at runtime in our Silverlight 4 application. I figured this should be as simple as loading a resource dictionary from XAML and merging it with the application's merged dictionaries. Here's my code so far:
var themeUri = new Uri(
"OurApp;component/Themes/Classic/Theme.xaml", UriKind.Relative);
var resourceInfo = GetResourceStream(themeUri);
using (var stream = resourceInfo.Stream)
{
using (var reader = new StreamReader(stream))
{
var xamlText = reader.ReadToEnd();
var dict = XamlReader.Load(xamlText) as ResourceDictionary;
Resources.MergedDictionaries.Add(dict);
}
}
Unfortunately, a XamlParseException
is raised during the call to XamlReader.Load
:
The attachable property 'Foo' was not found in type 'Bar'.
This attached properly is properly declared, and the namespace declaration in the XAML correctly references the required namespace. The attached property XAML works just fine if loaded into the merged dictionaries declaratively through the App.xaml markup.
Here's an abbreviated copy of the XAML which I'm trying to load at runtime:
<ResourceDictionary xmlns:u="clr-namespace:Company.Product.Utils"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style x:Key="ControlPanelStyle" TargetType="ContentControl">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ContentControl">
<Grid Margin="0" u:Bar.Foo="True">
<!-- Stuff and things -->
<ContentPresenter Content="{TemplateBinding Content}" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
Why is the reference to the attached property not working when loading XAML at runtime when it is working just fine when "statically" loaded?