wpf Generic.xaml 有什么特别之处?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1228875/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 20:48:47  来源:igfitidea点击:

What is so special about Generic.xaml?

wpfxamlthemes

提问by devuxer

I've been trying to figure out how to organize my ResourceDictionary files for reuse and sharing with other members of my team.

我一直在试图弄清楚如何组织我的 ResourceDictionary 文件,以便重复使用并与我团队的其他成员共享。

I keep coming across "Generic.xaml", but if I look on MSDN for Generic.xaml or just do a Google search, I only seem to get blog posts and forum questions that happen to mention it--I can't seem to hit upon anything really authoritative and clear.

我不断遇到“Generic.xaml”,但是如果我在 MSDN 上查看 Generic.xaml 或只是进行 Google 搜索,我似乎只会收到碰巧提到它的博客文章和论坛问题——我似乎无法找到任何真正权威和明确的东西。

What is the difference between Generic.xaml and MyRandomlyNamedResourceDictionary.xaml? It seems like either way, I have to reference ResourceDictionaries stored in libraries with the Source attribute. E.g.,:

Generic.xaml 和 MyRandomlyNamedResourceDictionary.xaml 有什么区别?似乎无论哪种方式,我都必须使用 Source 属性引用存储在库中的 ResourceDictionaries。例如,:

<Application.Resources>
    <ResourceDictionary
        Source="/CommonLibraryWpfThemes;component/Themes/Generic.xaml"
</Application.Resources>

So what advantage does Generic.xaml provide exactly? Does it have any purpose if I'm not trying to give my application multiple "looks" (i.e., if I have only one theme)?

那么 Generic.xaml 究竟提供了什么优势呢?如果我不想给我的应用程序多个“外观”(即,如果我只有一个主题),它是否有任何目的?

采纳答案by Phil Devaney

Every Control in WPF has a default Style that provides, among other things, the Control's default ControlTemplate. WPF looks for the default style in a special resource dictionary in the Themes folder in the same assembly as the control. The key for the default style is provided by the Control.DefaultStyleKeydependency property, the default value of which is overridden in each sub-class of Control.

WPF 中的每个控件都有一个默认样式,其中提供控件的默认ControlTemplate. WPF 在与控件相同的程序集中的 Themes 文件夹中的特殊资源字典中查找默认样式。默认样式的键由Control.DefaultStyleKey依赖属性提供,其默认值在 Control 的每个子类中被覆盖。

The name of the resource dictionary depends on the current Windows theme e.g. on Vista using the Aero theme, the dictionary is called Aero.NormalColor.xaml, on XP using the default theme it is Luna.NormalColor.xaml. If the style is not found in the theme dictionary, it looks in Generic.xaml i.e for controls whose look doesn't depend on the theme.

资源字典的名称取决于当前的 Windows 主题,例如在使用 Aero 主题的 Vista 上,该字典称为 Aero.NormalColor.xaml,在使用默认主题的 XP 上它是 Luna.NormalColor.xaml。如果在主题字典中找不到样式,它会在 Generic.xaml 中查找,即外观不依赖于主题的控件。

This only applies to any custom controls you have defined i.e. classes derived from Control, directly or indirectly. You can change the default style for a standard control by deriving from it and calling DefaultStyleKeyProperty.OverrideMetadatain the static constructor, but you then have to supply the full style including ControlTemplate.

这仅适用于您定义的任何自定义控件,即直接或间接从 Control 派生的类。您可以通过从标准控件派生并调用DefaultStyleKeyProperty.OverrideMetadata静态构造函数来更改标准控件的默认样式,但是您必须提供包括 ControlTemplate 在内的完整样式。

Note that you can tell WPF to look in an external assembly for your default style by using the ThemeInfo attribute. The external assembly must be named <YourAssembly>.<ThemeName>.dll e.g. PresententationFramework.Aero.dll.

请注意,您可以使用 ThemeInfo 属性告诉 WPF 在外部程序集中查找默认样式。外部程序集必须命名为<YourAssembly ><ThemeName >.dll 例如 PresententationFramework.Aero.dll。

回答by Kenan E. K.

For a generic.xamlfile (case insensitive) to be something special, two conditions must be met:

要使generic.xaml文件(不区分大小写)成为特殊文件,必须满足两个条件:

  • It must be in the Themes sub-root folder in the project
  • The assembly must be marked with the ThemeInfoAttribute(usually in AssemblyInfo.cs)
  • 它必须在项目的 Themes 子根文件夹中
  • 程序集必须标有ThemeInfoAttribute(通常在AssemblyInfo.cs

Then it serves as the default lookup location for any default styles you wish to apply to your Controls. Note also that for a style to be the default it must declare both its TargetType and x:Key as the Type of Control which is to be styled.

然后,它用作您希望应用于控件的任何默认样式的默认查找位置。另请注意,要使样式成为默认样式,它必须将其 TargetType 和 x:Key 声明为要设置样式的控件类型。

If you wish to add entire themes and theme switching to your application, that is accomplished with some coding, this technique merely defines the default resource dictionary.

如果您希望将整个主题和主题切换添加到您的应用程序中,这是通过一些编码完成的,此技术仅定义默认资源字典。