2011-10-12 11 views
0

爲了清理我的代碼,我試圖將我的app.xaml拆分爲單獨的資源字典。這個工程在運行時,而不是在設計時:Xaml Design View無法找到後臺筆刷

在App.xaml中

<Application.Resources> 
     <ResourceDictionary> 
     <ResourceDictionary.MergedDictionaries> 
      <ResourceDictionary Source="/;component/Theme/Colors.xaml" /> 
      <ResourceDictionary Source="/;component/Theme/Styles.xaml" /> 
     </ResourceDictionary.MergedDictionaries> 
     </ResourceDictionary> 
    </Application.Resources> 

Colors.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 

    <SolidColorBrush x:Key="backgroundBrush" Color="Gold"/> 
</ResourceDictionary> 

Styles.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 

     <Style TargetType="StatusBar"> 
     <Setter Property="Background" Value="{StaticResource backgroundBrush}" /> 
     </Style> 
    </ResourceDictionary> 

主窗口的剪斷剪斷.xaml

<Window x:Class="test.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="test" Width="800" Height="600" > 
    <StatusBar Name="statusBar" DockPanel.Dock="Bottom"> 
     <StatusBarItem Content="{Binding statusMessage}" /> 
    </StatusBar> 

DesignView出現錯誤: 錯誤8'{DependencyProperty.UnsetValue}'對屬性'Background'不是有效值。 C:\回到名單Daten \ DOTNET的\測試\測試\ MainWindow.xaml 123

如果我把backgroundBrush直接進入App.xaml中,像這樣:

<Application.Resources> 
     <ResourceDictionary> 
     <ResourceDictionary.MergedDictionaries> 
      <ResourceDictionary Source="/;component/Theme/Colors.xaml" /> 
      <ResourceDictionary Source="/;component/Theme/Styles.xaml" /> 
      <ResourceDictionary> 
       <SolidColorBrush x:Key="backgroundBrush" Color="Gold"/> 
      </ResourceDictionary> 
     </ResourceDictionary.MergedDictionaries> 
     </ResourceDictionary> 
    </Application.Resources> 

DesignView沒有問題。

那麼有沒有辦法告訴DesignView在哪裏可以找到backgroundBrush,如果把這個筆刷放入單獨的資源字典中?

+0

您的ResourceDictionary文件是否位於同一個程序集或不同的程序集中?如果資源字典在同一個程序集中,則在我的最後工作正常。 –

+0

主要部件中的所有內容。 – MTR

回答

5

多數民衆贊成在StaticResource沒問題。它需要分層使用共享\合併\直接資源字典明確解決的資源密鑰。

有兩個選項...

合併Colors.xaml辭典Styles.xaml

OR

Styles.xaml參考使用DynamicResource的bursh。

+0

-Even StaticResource在我的最後工作正常。所以,我猜問題是在別的地方。 –

+0

好的,DynamicResource解決了這個問題。但要了解,請告訴我有什麼不同,會有什麼影響? – MTR

+0

dynmaic資源直到運行時才使用其「x:Key」解析。如果過度使用DynamicResource,可能會使應用程序變慢。但他們gr8動態地改變應用程序皮膚(彩色主題)。 –

1

如果資源的位置與MainWindow所在的組件不同,並且一個字典引用另一個字典。在這種情況下,參考未解決。如果您的目標框架爲4.0,則此漏洞已在Microsoft網站上報告。不過他們已經提供了一個解決方法。只需添加在您的資源字典空的風格,它會正常工作這樣的 -

<Application.Resources> 
    <ResourceDictionary> 
    <ResourceDictionary.MergedDictionaries> 
     <ResourceDictionary Source="/;component/Theme/Colors.xaml" /> 
     <ResourceDictionary Source="/;component/Theme/Styles.xaml" /> 
    </ResourceDictionary.MergedDictionaries> 
    <Style TargetType="{x:Type Window}"/> 
    </ResourceDictionary> 
</Application.Resources> 

如需進一步refernce請看看這個鏈接 - https://connect.microsoft.com/VisualStudio/feedback/details/555322/global-wpf-styles-are-not-shown-when-using-2-levels-of-references#details

+0

沒有幫助。我的資源字典都駐留在主Assambly中,我只在DesignView中出現此錯誤。在運行時一切都很好。 – MTR

1

嘗試

{StaticResource ApplicationPageBackgroundThemeBrush} 

的狀態欄背景值