2009-06-10 48 views
5

我開始使用WPF(.net 3.5 sp1,僅限VS,尚未混合)開發桌面應用程序。XAML:如何定義要用於多個項目的數據模板和樣式

我在這一點上,我有幾個庫中的一些通用的可重用組件。

在哪裏可以定義我的樣式&數據模板,以便它們可以在多個項目中重複使用,這樣我可以擁有一致的外觀和感覺?

我已經看了ResourceDictionaries,但我不確定那

  1. 他們是什麼,我需要
  2. 如果他們是我需要什麼,我怎麼能 「進口」他們到其他項目和 在Xaml中引用它們的內容。

感謝,

回答

10

ResourceDictionary是要走的路,您可以在項目之間複製包含資源字典的xaml文件,或者將它們編譯爲您將從項目中引用的DLL。

要引用同一項目中的字典,請在App.xaml中添加這樣的內容(在這種情況下,我將資源保留在ControlStyles文件夾中)。

<Application.Resources> 
    <ResourceDictionary> 
     <ResourceDictionary.MergedDictionaries> 
      <ResourceDictionary Source="ControlStyles/Colors.xaml"/> 
      <ResourceDictionary Source="ControlStyles/Window.xaml"/> 
      <ResourceDictionary Source="ControlStyles/Button.xaml"/> 
     </ResourceDictionary.MergedDictionaries> 
    </ResourceDictionary> 
</Application.Resources> 

如果它們編譯成不同的dll,你可以使用這個語法(如果樣式DLL被稱爲StyleAssembly,單詞「組件」,實際上是語法的一部分,而不是一個目錄名):

<Application.Resources> 
    <ResourceDictionary> 
     <ResourceDictionary.MergedDictionaries> 
      <ResourceDictionary Source="pack://application:,,,/StyleAssembly;component/ControlStyles/Colors.xaml"/> 
      <ResourceDictionary Source="pack://application:,,,/StyleAssembly;component/ControlStyles/Window.xaml"/> 
      <ResourceDictionary Source="pack://application:,,,/StyleAssembly;component/ControlStyles/Button.xaml"/> 
     </ResourceDictionary.MergedDictionaries> 
    </ResourceDictionary> 
</Application.Resources> 
+0

Yer only a Star,thanks :) – 2009-06-10 21:09:36

0

你想合併的資源字典,描述here

3

@Nir是正確的,我喜歡做的事,以及唯一與此速記更換

<ResourceDictionary Source="pack://application:,,,/StyleAssembly;component/ControlStyles/Colors.xaml"/> 

<ResourceDictionary Source="/StyleAssembly;component/ControlStyles/Colors.xaml"/> 

它只是看起來更清潔,運行時將前綴pack:// application:,,,當它試圖找到資源。

相關問題