Shai UI
Shai UI

Reputation: 51958

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

Please let me know the difference between App.xaml and Generic.xaml, I'm confused between these two!

Upvotes: 5

Views: 5162

Answers (4)

Pavlo Glazkov
Pavlo Glazkov

Reputation: 20756

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?

Upvotes: 7

Aaron McIver
Aaron McIver

Reputation: 24723

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.

Upvotes: 2

Brian Driscoll
Brian Driscoll

Reputation: 19635

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.

Upvotes: 1

Emond
Emond

Reputation: 50682

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.

Upvotes: 1

Related Questions