我最近将我的Silverlight应用程序拆分成了几个较小的项目。
我将包含样式的所有资源字典移动到一个单独的项目("Application.Themes"),然后从我的主项目的App.xaml文件中引用这些资源字典。
这对于主项目来说很好,但是引用这些资源字典中的样式的所有其他项目在设计器中抛出“对象引用未设置为对象实例”的异常,尽管它们编译和运行时没有任何问题,并且具有正确的样式。
我已经向每个单独的项目添加了一个App.xaml文件,它引用与我的主App.xaml文件相同的字典,但这没有改变任何东西。
有没有一种正确的方法可以从另一个项目引用资源字典,从而允许使用设计器?
编辑:
以下是更多信息和一些代码片段,以演示我遇到的问题。
我有一个名为“Themes”的样式项目,在此项目中,我有几个字典,定义了项目的所有样式。
在我的主App.xaml中,我有以下内容:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Themes;component/Styles/CoreStyles.xaml"/>
<ResourceDictionary Source="/Themes;component/Styles/Styles.xaml" />
</ResourceDictionary.MergedDictionaries>
如果我在主项目中引用样式,它们可以正常工作。但是对于任何其他项目,即使这些项目引用了Themes项目,它们也无法正常工作。
我尝试在每个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>
我的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>