2010-11-02 24 views
0

說我有一個標準按鈕的主題文件Button.xaml。如何在組件設計器中隱式應用主題?

通過合併到應用程序資源字典中,它可以隱式應用於應用程序項目設計器中。

但我將Button.xaml移動到組件項目後,組件項目設計器不能隱式應用主題文件。

我怎樣才能使該主題文件隱式地在組件項目中工作?

更新: 的Themes.xaml如下

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

<ResourceDictionary.MergedDictionaries> 
<ResourceDictionary Source="pack://application:,,,/StyleLibrary;component/Themes/Shared.xaml" /> 
    <ResourceDictionary Source="pack://application:,,,/StyleLibrary;component/Themes/Button.xaml" /> 
    <ResourceDictionary Source="pack://application:,,,/StyleLibrary;component/Themes/ComboBox.xaml" /> 
... 




<Application x:Class="ButtonStyleTest.App" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      StartupUri="MainWindow.xaml"> 
    <Application.Resources> 
     <ResourceDictionary> 
      <ResourceDictionary.MergedDictionaries> 
       <ResourceDictionary Source="pack://application:,,,/StyleLibrary;component/Themes.xaml"/> 
      </ResourceDictionary.MergedDictionaries> 
     </ResourceDictionary> 
    </Application.Resources> 
</Application> 

如果我使用合併Theme.xaml,按鈕看在設計師正常,但在運行時失敗。 但是,如果我將Button.xaml,ComboBox一個接一個地合併,它對於設計和運行時都是正常的。

+0

這是usaully在我的情況下,一些新的模塊,應用項目的測試,然後將它建成後搬進組件項目。這將節省一些編譯時間。 – user244980 2010-11-02 13:21:06

回答

0

新的例子。

假設您有一個名爲StyleLibrary的庫。
在這個圖書館有一個名爲Button.xaml ResourceDictionary中,看起來像這樣

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 
    <Style TargetType="{x:Type Button}" > 
     <Setter Property="Width" Value="75" /> 
     <Setter Property="Height" Value="23" /> 
    </Style> 
</ResourceDictionary> 

在應用程序,我們再增加一個參考StyleLibrary和App.xaml中,我們添加此。

<Application x:Class="ButtonStyleTest.App" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      StartupUri="MainWindow.xaml"> 
    <Application.Resources> 
     <ResourceDictionary> 
      <ResourceDictionary.MergedDictionaries> 
       <ResourceDictionary Source="pack://application:,,,/StyleLibrary;component/Button.xaml"/> 
      </ResourceDictionary.MergedDictionaries> 
     </ResourceDictionary> 
    </Application.Resources> 
</Application> 

此樣式將應用於運行時和設計器中的應用程序和庫中。

左邊是我的MainWindow。有一個自己的Button,然後是一個TestLibrary的UserControl,它有兩個Button。右邊是來自TestLibrary的一個窗口,其中包含一個來自TestLibrary的UserControl,所有按鈕似乎都具有應用的樣式。

alt text

+0

謝謝Meleak。 – user244980 2010-11-02 12:59:14

+0

我的問題是,在應用程序項目中,不需要在UserControl標記中分配資源。它只是隱含地應用主題。但是,如果在組件級別,它是通過額外添加資源標記來應用主題的唯一方法嗎? – user244980 2010-11-02 13:07:31

+0

更新了我的回答 – 2010-11-02 13:45:06

相關問題