WPF:App.xaml和Generic.xaml有什么区别?

5

请告诉我App.xaml和Generic.xaml之间的区别,我对这两个很困惑!

4个回答

7

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 有什么特殊之处?


2

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>

这样可以将你的样式隔离在给定的XAML文件中,以便进行管理,并在运行时将该文件合并到应用程序中使用。
"generic.xaml" 被用作自定义控件默认样式的容器。当解析给定类型的样式时,框架会在 "Themes" 目录中查找 "generic.xaml"。

1

App.xaml 用于应用程序范围的资源,始终被使用。

Generic.xaml 用于自定义控件的模板和样式,在控件级别未指定其他样式或模板时将被使用。


1

App.xaml 是存放应用级资源的容器。

Generic.xaml 是一个针对所有不基于自定义或默认主题的控件的资源文件。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接