簡體   English   中英

如何將Application.Resources移至外部程序集?

[英]How to move Application.Resources to external assembly?

假設我具有以下內容的App.xaml

[...]
<Application.Resources>
  <Style TargetType="{x:Type CheckBox}">
    <Setter Property="BorderBrush" Value="{StaticResource Theme.CheckBox.Border}" />
    <Setter Property="Foreground" Value="{StaticResource Theme.Foreground}" />
  </Style>
</Application.Resources>
[...]

現在,我想將這些定義移至外部程序集。 這樣,我可以在整個應用程序中重用WPF控件和窗口樣式。 例如,我可以創建NuGet包並在幾秒鍾內安裝這些樣式。

您可以創建一個簡單的classLibrary項目,在單獨的資源詞典中定義所有樣式和模板等(推薦),然后將所有這些詞典合並為一個詞典。

要使用該特定程序集中的樣式,只需在您的App.Xaml文件中對其進行引用

以下幾張照片應該可以讓您全面了解

在此處輸入圖片說明

我習慣於為每種style創建一個單獨的ResourceDictionary (以方便訪問),所有這些資源字典都合並在ResourceLibrary.Xaml中

在此處輸入圖片說明

現在要引用該ControlLibraryAssembly (在添加對它的引用之后),在您的app.xaml文件中添加

 <Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="/ControlLibrary;component/ResourcesDictionaries/ResourceLibrary.xaml"/>
        </ResourceDictionary.MergedDictionaries>            
    </ResourceDictionary>        
</Application.Resources>

您需要進行資源組裝。 在這里您可以了解如何創建它: https : //msdn.microsoft.com/zh-cn/library/aa984332(v=vs.71).aspx

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM