我试图弄清楚如何在运行时以编程方式在Silverlight 4应用程序中应用主题.我认为这应该像从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:
The attachable property ‘Foo’ was not found in type ‘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时附加属性的引用在“静态”加载时工作得很好?
解决方法
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。