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
Related
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!)
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..
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.
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"/>
I'm moving a WPF application from VS 2010 to VS 2012.
I have a main executable which contains the App.xaml and a separate class library for all the WPF stuff.
In VS2012 (SP3) the XAML designer does not show any styles which are in a MergedResourceDictionary defined in App.xaml, VS 2010 does...
For testing purposes I moved App.xaml to my class library. After that the VS 2012 designer showed everything correctly.
Since class libraries cannot contain an App.xaml this is not a solution.
Any ideas?
My App.xaml looks like the following:
<Application x:Class="MyApplication.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Startup="AppStartup">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/OtherAssemblyName;component/Resources/Resource1.xaml" />
<ResourceDictionary Source="pack://application:,,,/OtherAssemblyName;component/Resources/Resource2.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
Know this is an old question, but answering it for anyone who may face something similar in the future.
The safest way to accomplish this is to create a new ResourceDictionary file, and add the MergedResourceDictionaries in it.
Then reference that ResourceDictionary in your user control/pages/windows - all of them, since the
<UserControl.Resources>
<ResourceDictionary Source="GlobalResources.xaml" />
<UserControl.Resources>