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

Please let me know the difference between App.xaml and 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.

Related

Forcing project in a solution to use its own XAML style

I have a solution with 5 projects.
Library
Shell
App 1
App 2
App 3
The shell and the apps all depend on the library.
The Shell is the application that runs.
I'm pretty novice, but I've never dealt with multiple projects in a solution and now I have style issues.
My main styles for each app are in its app.xaml file however all 3 apps seem to use the style from the app.xaml of the Shell project.
I've tryied putting:
Library/Styles/app1_style
for each style and then referencing that style in the app.xaml of each app:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/Library;component/Styles/app1_Styles.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
This doesn't seem to work at all.
How can I :
Make sure I use each apps specific stylesheet when I run the shell project
Make sure they can also see these styles at design time.
(I've tried deleting styles from app.xaml and then at design time everything is default but at runtime it then uses the style sheets!)

How to reference/use Resource Dictionary between multiple projects inside solution

I am building a WPF desktop app with .net framework 4.7.2. and need some help for merging resource dictionaries.
I have 1 solution with 2 projects:
Main parent project (ProcesingDesktopHub) which only has a nav bar and depending on user click its injecting a view/viewmodel from child project (DopisiUzPovrate).
Inside the parent project I have created a ResourceDictionary.xaml with some colors etc. and I want to be able to propagade that dictionary to the child project so it can be used if I run the child project separately from the parent (testing, designing etc.)
I have tried putting this inside the child but it does not work:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://ProcesingDesktopHub:,,,/ProcResourceDictionary.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
I hope I described my question correctly.
A solution cannot have circular references between projects.
Since the parent project refers to the child, then, accordingly, the child cannot refer to the parent in any way.
One of the solutions: move the shared resources (if you need not only resources, but also types, controls, etc.) to a third project that will be referenced by the parent and child projects.
Second variant: if you do not provide for the use of the child project outside the parent project, then you can use the design-time resource creation technique described here: https://stackoverflow.com/a/17712041/13349759
And in runtime, the resources of the parent project will be used.
Design time sounds good but I would need to run the parent project for debugging purposes each time if I understood it correctly.
If you want to separately debug a child project, then creating a third project with shared resources will be the best varaint.
In addition, if you ALWAYS use the parent project only together with the child, then pay attention to the answer from #MuhammadSulaiman.
Move ResourceDictionary.xaml to child project (i.e. DopisiUzPovrate), and the structure of resources in ProcesingDesktopHub (i.e. Application.xaml) would be like this
<Application>
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/DopisiUzPovrate;component/Path/To/ResourceDictionary.xaml" />
</ResourceDictionary.MergedDictionaries>
<!-- Converters, Colors and styles -->
</ResourceDictionary>
</Application.Resources>
</Application>
Update: In case you have one application (ex. ProcesingDesktopHub) and several modules (DopisiUzPovrate1, DopisiUzPovrate2, etc...), and all of these would use shared resources, then you have to create a SharedModule that hosts the shared resources and once you reference the ResourceDictionary.xaml in desktop application, it'd be available to the modules and application at runtime..

VSIX WPF Wizard Instead of Forms

I'm working on an extension for Visual Studio and I'm currently looking at using WPF instead of Forms for the template wizard.
I've got it all working with Forms but I much prefer the look of and working with WPF, but I'm not sure if what I'm trying to do is possible - or how I should go about doing it properly.
My VSIX consists of your typical layout, it's a solution with 3 projects;
1. MyProjectTemplate (C# Project Template)
2. MyProjectVSX (C# VSIX Template)
3. MyWizardWPF (WPF App)
I'm also using MahApps.Metro to make it look a bit sleeker. Added (via NuGet) and referenced by both MyProjectVSX and MyWizardWPF.
And while it works - the WPF shows up when I go to create a template - it is completely and utterly missing any styles. It basically just looks like a Windows 95 application.
I'm assuming this is related to the App.xml and it being unable to locate it. But I'm at a complete loss at how to tie all of this together. I get a bunch of warnings that it was unable to find various style related things. For example;
System.Windows.ResourceDictionary Warning: 9 : Resource not found; ResourceKey='MahApps.Brushes.IdealForeground'
System.Windows.ResourceDictionary Warning: 9 : Resource not found; ResourceKey='MahApps.Brushes.ThemeForeground'
I've tried playing around with the ResourceDictionary URIs, copying the App.xml to my VSX project. Tried building it as a class library instead of an application but to no avail.
Is what I'm trying to achieve here feasible...?
Well I was able to reference MahApps.Metro. Seems like I was vastly over-looking it all. I'm still not sure if this is the right way to do it, however. But it works.
I referenced MahApps.Metro inside the Window.xaml itself instead of via the App.xaml, like so;
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<!-- MahApps.Metro resource dictionaries. Make sure that all file names are Case Sensitive! -->
<ResourceDictionary Source="/MahApps.Metro;component/Styles/Controls.xaml" />
<ResourceDictionary Source="/MahApps.Metro;component/Styles/Fonts.xaml" />
<!-- Accent and AppTheme setting -->
<ResourceDictionary Source="/MahApps.Metro;component/Styles/Themes/Dark.Blue.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
This also works if I create a WPF User Control which will make the overall project a bit less bloated - but the designer will not be using MahApps.Metro styles. I have yet to find a fix for this.

How to set the theme for a c# .net WPF application

I has been noticed that when someone runs my application from windows 10 (I made it using windows 7) that buttons become unaligned, the only reason I can think for this happening is because of the different theme being defaulted to while being ran on the different operating systems.
How would I go about setting the default theme rather than allow it to choose depending on the operating system it is running on?
A lot of other similar questions reference a app.xaml file? but i don't seem to have this, is this auto-generated or something I would have to add myself?
The default templates of the WPF controls look different on different versions of Windows.
If you add a reference to PresentationFramework.Aero.dll and set its Copy Local property to true in Visual Studio, you can apply a Windows 7 theme your application by adding a merged ResourceDictionary into your App.xaml:
<Application ...>
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/PresentationFramework.Aero;component/themes/Aero.NormalColor.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
Besides Aero, there are some other themes (and their corresponding assemblies) available as well:
<ResourceDictionary Source="/PresentationFramework.Aero;component/themes/Aero.NormalColor.xaml"/>
<ResourceDictionary Source="/PresentationFramework.Classic;component/themes/Classic.xaml"/>
<ResourceDictionary Source="/PresentationFramework.Royale;component/themes/Royale.NormalColor.xaml"/>
<ResourceDictionary Source="/PresentationFramework.Luna.Homestead;component/themes/Luna.Homestead.xaml"/>
<ResourceDictionary Source="/PresentationFramework.Luna.Metallic;component/themes/Luna.Metallic.xaml"/>
<ResourceDictionary Source="/PresentationFramework.Zune;component/themes/Zune.NormalColor.xaml"/>

How to create a solution wide ResouceDictionary

I am working with Prism modules. That means I have a lot of projects inside my solution and I would like to share my WPF styles across all of them.
Currently I am simply adding them to all my xaml files.
As Example:
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Pack://application:,,,/MyProject.Shared;component/Styles/Colors.xaml" />
</ResourceDictionary.MergedDictionaries>
<ResourceDictionary>
<UserControl.Resources>
I was wondering if there is a better solution for this problem.
Edit: The "possible duplicate" is actually what I am doing at the moment.
I want to get rid of this repetitive code snippet in each of my UserControls. Since most of my projects are UserControlLibraries / Prism Modules they don´t have a kind of App.xaml where I can define the ResourceDictionaries.
A default WPF project should have a App.xaml file. Assuming your project still has that, you can add your Resource Dictionaries there under the Application.Resources tag.
see: https://learn.microsoft.com/en-us/dotnet/framework/wpf/app-development/how-to-use-an-application-scope-resource-dictionary

Categories

Resources