我正在尝试在我们的Silverlight 4应用程序中在运行时以编程方式应用主题。我认为这应该很简单,只需从XAML加载资源字典并将其合并到应用程序的合并字典中即可。以下是迄今为止的代码:
为什么在运行时加载XAML时,附加属性的引用无法正常工作,而在“静态”加载时却可以正常工作?
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);
}
}
很不幸,在调用XamlReader.Load
时,会触发一个XamlParseException
:
附加属性'Foo'在类型'Bar'中未找到。
这个附加属性是正确声明的,在XAML中的命名空间声明也正确地引用了所需的命名空间。如果通过App.xaml标记声明式地加载附加属性的XAML,则可以正常工作。
这是我正在尝试动态加载的XAML的缩写副本:
<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>
为什么在运行时加载XAML时,附加属性的引用无法正常工作,而在“静态”加载时却可以正常工作?