WPF程序在Windows 7下应用Windows 8主题

这篇博客介绍如何在Windows 7下应用Windows 8的主题。

首先我们先看一个很常见的场景,同样的WPF程序(样式未重写)在不同的操作系统上展示会有些不同。这是为什么呢?WPF程序启动时会加载与操作系统对应的主题。下面将列出常见的OS主题与WPF主题的关系。

针对不同操作系统的主题,WPF程序会加载不同的主题assembly。这样就形成了同样的XAML代码在不同的OS中展示效果。

回到这篇博客的主题,为Windows 7下的WPF程序应用Windows 8主题。通过上面的表格,可以知道,如果我们能够给WPF程序应用上PresentationFramework.Aero2.dll的主题就可以了。

1. WPF程序中添加PresentationFramework.Aero2.dll引用;

2. 将PresentationFramework.Aero2.dll的Copy Local属性设置为True;

3. 在App.xaml中添加下面的Resource;

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="/PresentationFramework.AeroLite;component/themes/AeroLite.NormalColor.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

此时在Windows 7环境下运行应用Windows 8主题的程序,结果如下:

 

PresentationFramework.Aero Theme相关的assembly可以在 C:\Windows\Microsoft.NET\assembly\GAC_MSIL目录下找到.

通过ILSpy获得WPF不同主题assembly对应的样式字典,

感谢您的阅读!

posted @ 2016-09-06 21:47  Yang-Fei  阅读(833)  评论(0编辑  收藏  举报