2012-06-20 46 views
3

我最近将我的silverlight应用程序拆分为几个较小的项目。在单独的项目中引用资源字典

我已经感动了所有包含我的风格成为一个单独的项目资源字典(“Application.Themes”),然后我我的主要项目中引用这些从我的App.xaml文件。

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

我已经添加了一个App.xaml文件到每个引用相同的字典作为我的主要App.xaml文件的个别项目,这也没有什么区别。

是否有从另一个项目,允许设计者引用资源字典正确的方式来使用呢?

编辑:

下面是一些更多的信息和一些代码片段展示我有

我已经叫款式项目这个项目中“主题”的问题,我有几本词典是定义项目的所有样式。

在我的主要的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.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

为样式/主题项目创建程序集,以便其他项目可以引用该程序集。 为了无论是在App.xaml中/ page.xaml合并这些样式到应用程序中使用MergedDictionaries

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

希望这是非常有用的。

+0

我真的不明白你在这里建议什么。我的主题项目已经在一个程序集中,并且已经被其他项目引用。 – Midimatt

+0

我已经更新了有关项目结构的更多详细信息。 – 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> 

我创建单独的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}"/> 

的了borderThickness,在它的工作对我来说这两种情况。 这是你想要的吗? 创建程序集时,是否将theme.xaml文件属性BuildAction设置为资源?

+0

我的问题是,除了我的主项目之外的所有项目都无法解决资源问题,主项目没有问题。例如。我有以下项目App.Main,App.Themes,App.Foo,App.Bar Foo和Bar不解决资源,尽管引用了App.Themes作为App.Main解决资源。 – Midimatt

+0

对我来说,它是工作我猜,因为testreturns是独立的project.Try与简单的单独project.best运气! – Sai