在另一个项目中引用资源字典

3

我最近将我的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>
2个回答

2
创建一个样式/主题项目的程序集,以便其他项目可以引用它。 为了将这些样式合并到应用程序中,可以在app.xaml/page.xaml中使用MergedDictionaries进行合并。
<Application.Resources>
 <ResourceDictionary>  
   <ResourceDictionary.MergedDictionaries>
         <ResourceDictionary Source="/Assembly;component/Stylesorthemes.xaml"/>             
     </ResourceDictionary.MergedDictionaries>
  </ResourceDictionary>
</Application.Resources> 

希望这对您有所帮助。

我不太明白你在这里提出的建议。我的主题项目已经是一个程序集,并且已经被其他项目引用了。 - Midimatt
我已经更新了我的初始问题,并提供了有关项目结构的更多细节。 - Midimatt

0
我创建了一个包含资源字典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>

我创建了一个名为testreturns的独立Silverlight项目。

第一种情况,在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,但它们无法解析资源,而App.Main可以解析资源。 - Midimatt
对我来说,我猜是因为testreturns是一个单独的项目,所以它可以工作。尝试使用简单的单独项目。祝你好运! - Sai

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接