2011-04-12 95 views
16

主題\ Generic.xaml:WPF - 資源不是來自Generic.xaml加載

<ResourceDictionary 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 
    <ResourceDictionary.MergedDictionaries> 
     <ResourceDictionary Source="WPF Commons;component/Controls/Layout/Foo/FooItem.xaml" /> 
    </ResourceDictionary.MergedDictionaries> 
</ResourceDictionary> 

控制\佈局\富\ FooItem.xaml:

<Style TargetType="{x:Type l:FooItem}"> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type l:FooItem}"> 
       <Border> 
        <ContentPresenter ContentSource="Header" /> 
       </Border> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 


如果我將整個樣式複製到我的usercontrol資源中,它工作正常。但是,如果我不這樣做,用戶控件顯示爲空。在Expression Blend 4中,我右鍵單擊並選擇了Edit Template>,但它不會讓我選擇Edit a Copy...,這導致我認爲某些內容嚴重錯誤,並且Generic.xaml未正確加載。我認爲它是Generic.xaml,因爲如果我刪除MergedDictionary調用並將xaml樣式直接複製/粘貼到Generic.xaml中,它仍然不起作用。

+0

您還可以擴大(下拉)在Solution Explorer中的特殊屬性節點,然後打開AssemblyInfo.cs中和添加上面的東西,你能告訴你的FooItem代碼?通常,UserControls不應該被重新設計(即設置Template屬性),因爲它不能按預期工作。 – CodeNaked 2011-04-12 17:15:57

+0

FooItem.cs現在爲空,我想確保自己的佈局正確並稍後在其他DependencyProperties和邏輯中緩慢添加。 – michael 2011-04-12 17:24:10

+0

FooItem從何而來? – CodeNaked 2011-04-12 17:30:09

回答

30

我要帶胡亂猜測,你改變你的AssemblyInfo.cs文件,要麼改變(或刪除)以下行:

[assembly: ThemeInfo(
    ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located 
    //(used if a resource is not found in the page, 
    // or application resource dictionaries) 
    ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located 
    //(used if a resource is not found in the page, 
    // app, or any theme specific resource dictionaries) 
)] 

你需要告訴你的ThemeInfo程序集。 :)從我的博客

+3

出現同樣的問題並使用上述屬性進行修復。在我的情況下,這個問題是由於最初並未設置爲WPF自定義控件庫的ac#庫項目(因此上述屬性未自動添加到程序集中)引起的,後來添加了一個自定義控件 – cordialgerm 2012-03-09 18:32:22

1

複製:在你需要的屬性\ AssemblyInfo.cs中開始http://zoomicon.wordpress.com/2012/06/10/what-to-do-if-generic-xaml-doesnt-get-loaded-for-wpf-control/

(注意,這不使用/在Silverlight需要): 使用System.Windows;

...

你要知道,如果該項目不顯示在解決方案資源屬性節點,你必須要麼使用做出了正確的模板(對於WPF自定義控件)一個新的項目,或向右單擊該項目,選擇屬性,然後按組裝信息按鈕並輸入一些虛擬值,然後單擊確定以創建屬性節點(也可以創建屬性子文件夾和AssemblyInfo.cs文件)。

如果缺少

+0

我也注意到如果您在用於控件Style的資源的XAML定義中有一個鍵,則它不會加載,您應該只使用Target屬性 - 可能會在內部創建一些隱式鍵 – 2014-09-04 14:17:55