【问题标题】:Referencing Resource Dictionaries in a separate project在单独的项目中引用资源字典
【发布时间】:2012-06-20 08:56:26
【问题描述】:

我最近将我的 silverlight 应用程序拆分为几个较小的项目。

我已将包含我的样式的所有资源字典移动到一个单独的项目(“Application.Themes”)中,然后我从我的主项目中的 App.xaml 文件中引用这些。

这适用于主项目,但是在这些资源字典中引用样式的所有其他项目在设计器中抛出“对象引用未设置为对象的实例”异常,尽管它们确实编译和运行没有任何问题并且正确的样式。

我已向每个单独的项目添加了一个 App.xaml 文件,该文件引用了与我的主 App.xaml 文件相同的字典,这没有任何区别。

是否有正确的方法来引用另一个项目中允许使用设计器的资源字典?

编辑:

这里有更多信息和一些代码 sn-ps 来演示我遇到的问题

我在这个项目中有一个名为“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>

【问题讨论】:

    标签: silverlight silverlight-5.0 resourcedictionary


    【解决方案1】:

    为样式/主题项目创建一个程序集,以便其他项目可以引用它。 为了在 app.xaml/page.xaml 中使用 MergedDictionaries 将这些样式合并到应用程序中

    <Application.Resources>
     <ResourceDictionary>  
       <ResourceDictionary.MergedDictionaries>
             <ResourceDictionary Source="/Assembly;component/Stylesorthemes.xaml"/>             
         </ResourceDictionary.MergedDictionaries>
      </ResourceDictionary>
    </Application.Resources> 
    

    希望这是有用的。

    【讨论】:

    • 我不太明白你在这里的建议。我的主题项目已经在程序集中,并且已被其他项目引用。
    • 我已经更新了我最初的问题,提供了有关项目结构的更多详细信息。
    【解决方案2】:

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

    我创建了单独的 silverlight 项目 testreturns

    case1.In 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>
    

    case2.用户控制级别

    <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 确实解析了资源。
    • 对我来说它正在工作,我猜是因为 testreturns 是单独的项目。尝试使用简单的单独项目。祝你好运!
    猜你喜欢
    • 2015-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多