如何将Application.Resources移动到外部程序集?

时间:2015-09-03 09:07:38

标签: c# wpf xaml app.xaml

假设我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包并在几秒钟内安装这些样式。

2 个答案:

答案 0 :(得分:3)

您可以在单独的资源词典中创建一个简单的classLibrary项目,定义所有样式和模板等(推荐),并将所有这些词典合并到一个词典中。

要使用特定程序集中的那些样式,只需在 App.Xaml 文件中引用它,

以下镜头应该为您提供全局视图

enter image description here

我习惯为每个ResourceDictionary创建单独的style(方便访问),所有这些资源词典都合并在 ResourceLibrary.Xaml

enter image description here

现在参考 ControlLibraryAssembly (在添加对它的引用之后),在 app.xaml 文件中添加

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

答案 1 :(得分:1)

您需要进行资源组装。在这里,您可以阅读如何创建它: https://msdn.microsoft.com/en-us/library/aa984332(v=vs.71).aspx

相关问题