Forcing WPF to use a specific Windows theme
来源:互联网 发布:易格家具淘宝代购假货 编辑:程序博客网 时间:2024/05/22 07:39
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 =newUri(“PresentationFramework.Aero;V3.0.0.0;31bf3856ad364e35;component\\themes/aero.normalcolor.xaml”,UriKind.Relative);
Resources.MergedDictionaries.Add(Application.LoadComponent(uri)asResourceDictionary);
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>
<ResourceDictionarySource=“/PresentationFramework.Aero;V3.0.0.0;31bf3856ad364e35;component\themes/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 sampleOrcas project)
pack://application:,,,/PresentationFramework.Aero;V3.0.0.0;31bf3856ad364e35;component\themes/aero.normalcolor.xaml
- Forcing WPF to use a specific Windows theme
- How to use 'cp' command to exclude a specific directory?
- nrf51822, How to use a vendor specific UUID?
- you need to use a theme.appcompat theme的解决方法
- You need to use a Theme.AppCompat theme的解决方案
- Forcing specific layers to compare in Ax 4.0x
- How to force NSLocalizedString to use a specific language iOS APP内国际化
- Unable to start activity ComponentInfo...You need to use a Theme.AppCompat theme
- java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this a
- java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this a
- java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this a
- java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this a
- java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this a
- java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this a
- java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this a
- java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this a
- java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this a
- 关于“you need to use a theme.appcompat theme (or descendant) with this activity”错误
- 第15周报告1 冒泡排序
- ASP.NET里的Session详细解释
- Smooth Pixel Scrolling in WPF ListView / ListBox
- 四极管:温度监控之 AVR 18B20
- 第15周报告2 操作字符
- Forcing WPF to use a specific Windows theme
- 理解 Xwindow
- hdu 3631 Shortest Path
- 四极管:AD转换之 AVR自带AD转换
- Using JavaScript partitioning
- 打造可复用可扩展javascript验证表单脚本
- sgu 326 Perspective(最大流)
- 关于Tomcat错误java.lang.NoClassDefFoundError: javax/el/ExpressionFactory的问题
- IIS6.0 + php