Forcing a Particular Theme in a WPF App
From your many comments, it appears that the Zune theme leaves WPF apps defaulting to the Classic theme for default control styles. This is as designed, but admittedly kind of a bummer. Luna Silver would probably be a closer match or even the blue variant of Royale.
A couple of people asked if they can get WPF to load a different theme when Zune is active. There isn’t a way to map this kind of indirection where one theme behaves like another one for all apps. There are a couple of opportunities to know about though.
First, it is possible to force your app to always use a particular theme. You do this by loading the resources for that theme at the root of your app. When you do this, resource lookups which would normally make it all the way to the system theme only make it to root of the app where they pick up the resources you’ve specified.
Here’s the syntax for forcing the Aero theme in a Window:
<Window
x:Class=”TestProject.Window1″
xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation”
xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml”>
<Window.Resources>
<ResourceDictionary
Source=”/presentationframework.aero;component/themes/aero.normalcolor.xaml” />
</Window.Resources>
</Window>
You need to have a reference to PresentationFramework.Aero.dll in order for this to work. To try this out, create a new project and make sure to add that assembly to your project.
The second opportunity to be aware of is your chance to respond to the Zune theme when creating a custom control. When you create a new control in VS by adding a “Custom Control (WPF)” to your project, VS generates a file called generic.xaml. This is the file you use to create the default style for your control. That file is called generic.xaml because it theme independent, i.e., it’s generic. You can, however, add other resource dictionaries to that same directory to that are theme aware. The naming scheme is [ThemeName].[ColorVariant] (thus, “aero.normalcolor” in the code above). If you added a resource dictionary called “Luna.Metallic” to your project, we would would only load it when the Silver variant of Luna was the active theme on the machine.
So, presumably you could add something like Zune.NormalColor to your themes directory and your control would become Zune aware. “Zune.NormalColor” is just my best guess as to the right naming convention. Let me know if it works.
November 8th, 2006 at 3:39 pm
使WPF程åºä½¿ç”¨ç‰¹å®šçš„ä¸»é¢˜æ ·å¼, 如Aero, Luna, Royale, Classic牅
WPF预设有Aero,Classic,Luna,Royale主题,WPF程åºä¼šæ ¹æ®Windows主题决定WPFç¨‹åºæ‰€ä½¿ç”¨çš„æŽ§ä»¶UIé£Žæ ¼,而且当Windowsä¸»é¢˜ä¸æ˜¯Aero,Luna或Ro……
November 8th, 2006 at 4:13 pm
i had been finding the solution for so long time~
aero in windows xp seems very cool!
November 23rd, 2006 at 11:15 pm
Hi!
Looks like this trick:
works only when “Enable the Visual Studio hosting process” option is set in the project settings.
And it works only under the debugger.
November 24th, 2006 at 12:39 pm
Here is another way to force a particular theme:
http://learnwpf.com/Posts/Post.aspx?postId=3f1f4b8b-b91a-442d-a531-919de70ac225
December 13th, 2006 at 6:39 pm
Great… but…
I’m under XP and would like to use the aero.normalcolor.xaml style…
But I cannot find the file “aero.normalcolor.xaml”, I have no Vista system installed.
Is it possible to download it somewhere ?
Thanks
December 15th, 2006 at 7:31 pm
Hai ,
can you give me an idea about how can i load vista theams from my WPF application, with key name , for example i need to apply template for a button from the vista themes.
i have loaded like this , but i need to apply this theme into my control like Button , Label .
Thanks ,
February 23rd, 2007 at 4:38 pm
i like the thim
June 10th, 2007 at 4:03 pm
[…] Forcing WPF to use a specific Windows theme Published 03 November 06 12:21 AM | aelij WPF comes with a few theme assemblies, one for each Windows theme (Luna, Royale and Aero and the fallback theme, Classic.) Usually the theme is loaded according to your current system theme, but if you want to create a consistent look for your application, you may want to force-load a specific one. To accomplish that, simply add the following code in your Application Startup event (this example shows how to use the Aero theme): Uri uri = new Uri(”PresentationFramework.Aero;V3.0.0.0;31bf3856ad364e35;component\themes/aero.normalcolor.xaml”, UriKind.Relative); Resources.MergedDictionaries.Add(Application.LoadComponent(uri) as ResourceDictionary); It’s important to specify the version and the public key token. Otherwise you’ll have to copy the theme assembly to the folder of your executable. The reason I’m adding it to the merged dictionaries collection is that I don’t want to lose other resources I added to the App.xaml file. I usually put this code in a try…catch block (with an empty catch) since it doesn’t really impair the application’s functionality if it fails to execute. Last note: From my experience, Windows Server 2003 always shows the Classic theme in WPF (even if you activate the Windows Themes service), so if you’re deploying applications for that platform, you may want to use this trick (you will also want to turn on your display adapter’s hardware acceleration and the DirectX accelerations, as they are disabled by default in 2003.) Edit: Robby Ingebretsen (notstatic.com) also blogged about this because the new Zune theme, which caused WPF to fallback to the Classic theme. However, he placed the code in XAML. Here is a version of that using merged dictionaries (which will allow you to add other resources to App.xaml): <Application.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source=”/PresentationFramework.Aero;V3.0.0.0;31bf3856ad364e35;componentthemes/aero.normalcolor.xaml” /> </ResourceDictionary.MergedDictionaries> <!– other resources go here –> </ResourceDictionary></Application.Resources> Update: The Orcas designer seems to be having problems with the relative URI. Using an absolute URI solves the issue: (I’ve also attached a sample Orcas project) pack://application:,,,/PresentationFramework.Aero;V3.0.0.0;31bf3856ad364e35;componentthemes/aero.normalcolor.xaml Filed under: windows vista, wpf, windows 2003, themes Attachment(s): ForcedTheme.rar […]
April 9th, 2008 at 9:49 am
Unlike Viagra, http://www.carroll.edu/boards/member.php?u=10591 - Generic Cialis is good for up to 36 hours after it’s taken - adding spontanaeity.
Is this a good thing? What do you think?