XAML:如何定义数据模板用于多个项目的样式 [英] XAML: How to define Data Templates & Styles to be used over several Projects

查看:60
本文介绍了XAML:如何定义数据模板用于多个项目的样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始使用 WPF(.net 3.5 sp1,仅使用 VS,尚未混合)开发桌面应用程序.

I'm starting to develop a Desktop application using WPF (.net 3.5 sp1, with VS only, not blend as yet).

我现在在几个库中有一些通用的可重用组件.

I'm at the point were I have some generic reusable components in several libraries.

我在哪里可以定义我的样式&数据模板,以便它们可以在多个项目中重复使用,这样我就可以拥有一致的外观和感觉?

Where can I define my Styles & Data Templates so that they are reusable across several projects, so I can have a consistent look and feel?

我看过ResourceDictionaries,但不确定

I've looked at ResourceDictionaries, but am unsure that

  1. 他们正是我所需要的
  2. 如果它们是我需要的,我该怎么做将它们导入"到其他项目中在 Xaml 中引用它们的内容.

谢谢,

推荐答案

ResourceDictionary 是可行的方法,您可以在项目之间复制包含资源字典的 xaml 文件,也可以将它们编译成您将从项目中引用的 DLL.

ResourceDictionary is the way to go, you can either copy an xaml file containing the resource dictionary between projects or compile them into a DLL you'll reference from your projects.

要在同一个项目中引用字典,您可以在 App.xaml 中添加类似的内容(在这种情况下,我将资源保存在 ControlStyles 文件夹中).

To reference dictionaries in the same project you add something like this to your App.xaml (in this case I keep my resources in the ControlStyles folder).

<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,组件"这个词实际上是语法的一部分,而不是目录名称):

If you compile them into a different dll you can use this syntax (if the styles dll is called StyleAssembly, the word "component" is actually part of the syntax and not a directory name):

<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>

这篇关于XAML:如何定义数据模板用于多个项目的样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆