2012-06-20 48 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