Silverlight 在单独的项目中引用资源词典
我最近将silverlight应用程序拆分为几个较小的项目 我已经将包含我的样式的所有资源字典移动到一个单独的项目(“Application.Themes”)中,然后从我的主项目中的App.xaml文件中引用它们 这对于主项目来说效果很好,但是在这些资源字典中引用样式的所有其他项目都会在设计器中抛出“对象引用未设置为对象实例”异常,尽管它们的编译和运行没有任何问题,并且样式正确 我已经在每个项目中添加了一个App.xaml文件,它引用了与我的主App.xaml文件相同的字典,这没有什么区别 是否有一种正确的方法来引用另一个项目中的资源字典,从而允许使用设计器 编辑: 这里有更多的信息和一些代码片段来演示我遇到的问题 我有一个名为“主题”的样式项目,在这个项目中,我有几个字典定义了项目的所有样式Silverlight 在单独的项目中引用资源词典,silverlight,silverlight-5.0,resourcedictionary,Silverlight,Silverlight 5.0,Resourcedictionary,我最近将silverlight应用程序拆分为几个较小的项目 我已经将包含我的样式的所有资源字典移动到一个单独的项目(“Application.Themes”)中,然后从我的主项目中的App.xaml文件中引用它们 这对于主项目来说效果很好,但是在这些资源字典中引用样式的所有其他项目都会在设计器中抛出“对象引用未设置为对象实例”异常,尽管它们的编译和运行没有任何问题,并且样式正确 我已经在每个项目中添加了一个App.xaml文件,它引用了与我的主App.xaml文件相同的字典,这没有什么区别 是否
<UserControl>
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Themes;component/Styles/CoreStyles.xaml"/>
<ResourceDictionary Source="/Themes;component/Styles/Styles.xaml" />
</ResourceDictionary.MergedDictionaries>
<!-- Additional Control Specific resources -->
</ResourceDictionary>
</UserControl.Resources>
<!-- The following resources are defined in Styles.XAML and don't resolve at design time and throw errors -->
<TextBlock Text="Header Test"
FontFamily="{StaticResource HeaderFontFamily}"
Foreground="{StaticResource StrongBrush}">
</UserControl>
在我的主App.xaml中,我有以下内容
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Themes;component/Styles/CoreStyles.xaml"/>
<ResourceDictionary Source="/Themes;component/Styles/Styles.xaml" />
</ResourceDictionary.MergedDictionaries>
如果我在主项目中引用样式,它们将正常工作。但是,它们不适用于任何其他项目,即使这些项目引用了项目主题
为了在设计时解析样式,我尝试将以下内容放在每个UserControl的开头,但是它仍然无法解析项目中的样式
<UserControl>
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Themes;component/Styles/CoreStyles.xaml"/>
<ResourceDictionary Source="/Themes;component/Styles/Styles.xaml" />
</ResourceDictionary.MergedDictionaries>
<!-- Additional Control Specific resources -->
</ResourceDictionary>
</UserControl.Resources>
<!-- The following resources are defined in Styles.XAML and don't resolve at design time and throw errors -->
<TextBlock Text="Header Test"
FontFamily="{StaticResource HeaderFontFamily}"
Foreground="{StaticResource StrongBrush}">
</UserControl>
My styles.xaml与此类似
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:behaviors="clr-namespace:Minerva.Presentation.Behavior;assembly=Minerva.Presentation"
xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation">
<SolidColorBrush x:Key="StrongBrush" Color="{Binding Source={StaticResource MetroColors}, Path=Palette.StrongColor}" />
<FontFamily x:Key="HeaderFontFamily">Segoe UI Light, Lucida Sans Unicode, Verdana</FontFamily>
</ResourceDictionary>
Segoe UI Light、Lucida Sans Unicode、Verdana
为样式/主题项目创建程序集,以便其他项目可以引用该程序集。
为了在app.xaml/page.xaml处使用MergedDictionaries将这些样式合并到应用程序中
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Assembly;component/Stylesorthemes.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
希望这是有用的 我创建了一个包含资源字典Theme.xaml的汇编测试 Theme.xaml代码
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Thickness x:Key="GeneralThickness">10</Thickness>
</ResourceDictionary>
10
我创建了单独的silverlight项目testreturns
案例1.在App.xaml中
<Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="testreturns.App"
>
<Application.Resources>
<ResourceDictionary >
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/test;component/Theme.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
案例2.用户控制级别
<UserControl.Resources>
<ResourceDictionary >
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/test;component/Theme.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
并使用它来设置按钮的边框厚度
<Button Height="50" Width="150" BorderThickness="{StaticResource GeneralThickness}"/>
在这两种情况下,它都对我有效。
这就是你想要的吗?
创建程序集时,您是否将theme.xaml文件属性BuildAction设置为Resource?我不太明白您在这里的建议。我的主题项目已在程序集中,并且已被其他项目引用。我已更新了我的初始问题,提供了有关项目结构的更多详细信息。我的问题是,除主项目外的所有项目都无法解决资源问题,主项目没有问题。例如,我有以下项目App.Main、App.Themes、App.Foo、App.Bar Foo和Bar不解析资源,尽管它们引用了App.Themes,其中as App.Main解析资源。对我来说,它正在工作,我猜是因为testreturns是单独的项目。尝试使用简单的单独项目。祝你好运!!