请告诉我App.xaml和Generic.xaml之间的区别,我对这两个很困惑!
App.xaml
是应用程序类的XAML部分,它是定义应用程序范围逻辑和资源的单一集中位置。而Generic.xaml
位于项目的Themes
目录中,是一个字典,您可以在其中为所有自定义控件定义默认样式。当Themes
文件夹中没有窗口主题特定的字典时,将使用此字典。例如,您可能会有以下Themes
目录结构:
MyProject
- Themes
- Generic.xaml // Default styles if current theme is non of the themes below
- Classic.xaml // Styles for “Classic” Windows 9x/2000 look on Windows XP.
- Luna.NormalColor.xaml // Styles for default blue theme on Windows XP.
- Luna.Homestead.xaml // Styles for olive theme on Windows XP.
- Luna.Metallic.xaml // Styles for silver theme on Windows XP.
- Royale.NormalColor.xaml // Styles for default theme on Windows XP Media Center Edition.
- Aero.NormalColor.xaml // Styles for default theme on Windows Vista
如果您希望您的自定义控件在任何 Windows 主题下都保持相同的外观,则只需创建 Generic.xaml。
因此,基本上应仅使用 Generic.xaml
来定义自定义控件的样式,而使用 App.xaml
来处理其他所有内容(例如,您的画刷、颜色等或标准控件的自定义样式)。
请参阅此问题的答案:Generic.xaml 有什么特殊之处?
App.xaml
用于应用程序范围的资源,因此可以包含对其他 XAML 资源的引用。
<Application
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" x:Class="App">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Themes/ValidationStyles.xaml"/>
<ResourceDictionary Source="Themes/ControlStyles.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
App.xaml 用于应用程序范围的资源,始终被使用。
Generic.xaml 用于自定义控件的模板和样式,在控件级别未指定其他样式或模板时将被使用。
App.xaml 是存放应用级资源的容器。
Generic.xaml 是一个针对所有不基于自定义或默认主题的控件的资源文件。