整理了一个WPF的所有Theme,写了一个ThemeSwitcher,用于切换主题

为你的程序切换主题,这个主题不是随着系统变化的,而是你程序专有的,

无论系统主题如何变化,你的程序就是当前这个德行----就是不变!

注意:

1.需要.net3.5 

2.当中的"OFFICE2007"主题需要Assergs.Windows.dll . 这个是比较不错的开源Office2007风格的框架.

项目地址: http://www.codeplex.com/appfx/Wiki/View.aspx?title=Home&version=17

3.其他的dll的位置都在源码文件中的注释写清楚了,敬请品尝!

 

ContractedBlock.gifExpandedBlockStart.gifCode
using System;
using System.Windows;
using Assergs.Windows;

namespace XMLSpy.WPF.Util{
    
/// <summary>
    
/// ThemeSwitcher所使用的主题枚举
    
/// </summary>
    [Flags]
    
public enum ThemeEnum{
        CLASSIC 
= 1,
        ROYALE 
= 2,
        LUNA 
= 4,
        LUNA_HOMESTEAD 
= 8,
        LUNA_METALLIC 
= 16,
        
/// <summary>
        
/// Vista默认主题
        
/// </summary>
        AERO = 32,
        
/// <summary>
        
/// Office2007主题
        
/// </summary>
        OFFICE2007 = 64
    }

    
/// <summary>
    
/// Theme切换
    
/// </summary>
    public class ThemeSwitcher{
        
/// <summary>
        
/// 切换Theme
        
/// </summary>
        
/// <param name="theme">Theme枚举</param>
        
/// <param name="element">FrameworkElement对象</param>
        public static void SwitchTheme(ThemeEnum theme, FrameworkElement element){
            element.Resources.MergedDictionaries.Add(GetThemeResourceDictionary(theme));
        }

        
//public static void UnloadTheme(ThemeEnum theme,FrameworkContentElement element) {
        
//    element.Resources.MergedDictionaries.Remove(GetThemeResourceDictionary(theme));
        
//}

        
public static ResourceDictionary GetThemeResourceDictionary(ThemeEnum theme){
            Uri uri 
= null;
            
//==================== OFFICE2007 ======================================
            
//Assergs.Windows.dll
            
//office2007
            if (theme == ThemeEnum.OFFICE2007){
                
return new OfficeStyle();
            }


            
switch (theme){
                    
//==================== CLASSIC ======================================
                    
//C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.0\PresentationFramework.Classic.dll
                    
//classic
                case ThemeEnum.CLASSIC:
                    uri 
=
                        
new Uri(
                            
"/PresentationFramework.Classic, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35;component/themes/classic.xaml",
                            UriKind.Relative);

                    
break;
                    
//==================== ROYALE ======================================
                    
//C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.0\PresentationFramework.Royale.dll
                    
//royale.normalcolor
                case ThemeEnum.ROYALE:
                    uri 
=
                        
new Uri(
                            
"/PresentationFramework.Royale, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35;component/themes/royale.normalcolor.xaml",
                            UriKind.Relative);
                    
break;
                    
//==================== LUNA ======================================
                    
//C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.0\PresentationFramework.Luna.dll
                    
//luna.normalcolor
                case ThemeEnum.LUNA:
                    uri 
=
                        
new Uri(
                            
"/PresentationFramework.Luna, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35;component/themes/luna.normalcolor.xaml",
                            UriKind.Relative);
                    
break;
                    
//luna.homestead
                case ThemeEnum.LUNA_HOMESTEAD:
                    uri 
=
                        
new Uri(
                            
"/PresentationFramework.Luna, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35;component/themes/luna.homestead.xaml",
                            UriKind.Relative);
                    
break;
                    
//luna.metallic
                case ThemeEnum.LUNA_METALLIC:
                    uri 
=
                        
new Uri(
                            
"/PresentationFramework.Luna, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35;component/themes/luna.metallic.xaml",
                            UriKind.Relative);
                    
break;
                    
//==================== AERO ======================================
                    
//C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.0\PresentationFramework.Aero.dll
                    
//aero.normalcolor
                case ThemeEnum.AERO:
                    uri 
=
                        
new Uri(
                            
"/PresentationFramework.Aero, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35;component/themes/aero.normalcolor.xaml",
                            UriKind.Relative);
                    
break;
            }
            
return Application.LoadComponent(uri) as ResourceDictionary;
        }
    }
}

 

 

转载于:https://www.cnblogs.com/xmlspy/archive/2008/08/07/1262486.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值