开发者

WPF: What's the difference between App.xaml and Generic.xaml?

开发者 https://www.devze.com 2023-02-10 05:04 出处:网络
Please let me know the difference between App.xaml and开发者_如何学Go Generic.xaml, I\'m confused between these two!App.xaml is a XAML part of the Application class - the single centralized place wher

Please let me know the difference between App.xaml and开发者_如何学Go Generic.xaml, I'm confused between these two!


App.xaml is a XAML part of the Application class - the single centralized place where you define application-wide logic and resources. While Generic.xaml, being located in the Themes directory of your project, is a dictionary where you define default styles for all your custom controls. This dictionary is used when there is no windows theme-specific dictionary in the Themes folder. For example, you might have the following structure of the Themes directory:

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

If you want you custom control to look the same on any windows theme, you need to create only Generic.xaml.

So, basically you should use Generic.xaml only to define styles for your custom control and App.xaml for everything else (e.g. your brushes, color, etc. or your own styles for standard controls).

See also the answer to this question: What is so special about Generic.xaml?


The App.xaml is used for application wide resources and is always used.

Generic.xaml is used for the templates and styles for Custom Controls and will be used when you do not specify an other style or template at control level.


App.xaml is the container for your application-level resources.

Generic.xaml is a resource file for all of your controls that are not based on a custom or default theme.


App.xaml is used for application wide resources and can therefore contain references to other XAML resources.

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

This allows you to isolate your styles in a given XAML file for manageability and will then make use of the file within the application since it is being merged into the application at runtime.

generic.xaml is used as the default the container for the default style of a custom control. The framework will look in the Themes directory for generic.xaml when resolving a style for a given type.

0

精彩评论

暂无评论...
验证码 换一张
取 消