WPF-SkinnedApplication

简介: WPF 皮肤

WPF 皮肤
1、新建两个ResourceDictionary的xaml,BlueSkin.xaml和YellowSkin.xaml;

<!--<SnippetBlueSkinMARKUP1>-->
<!-- Blue Skin -->
<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:SkinnedApplication">
  <Style TargetType="{x:Type Button}">
    <Setter Property="Background" Value="Blue" />
  </Style>
  <Style TargetType="{x:Type local:ChildWindow}">
    <Setter Property="Background" Value="Blue" />
  </Style>
  <Style TargetType="{x:Type local:MainWindow}">
    <Setter Property="Background" Value="Blue" />
  </Style>
</ResourceDictionary>
<!--</SnippetBlueSkinMARKUP2>-->
<!--<SnippetYellowSkinMARKUP1>-->
<!-- Yellow Skin -->
<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:SkinnedApplication">
  <Style TargetType="{x:Type Button}">
    <Setter Property="Background" Value="Yellow" />
  </Style>
  <Style TargetType="{x:Type local:ChildWindow}">
    <Setter Property="Background" Value="Yellow" />
  </Style>
  <Style TargetType="{x:Type local:MainWindow}">
    <Setter Property="Background" Value="Yellow" />
  </Style>
</ResourceDictionary>
<!--</SnippetYellowSkinMARKUP2>-->

2、App.xaml添加 Startup="App_Startup";

<Application x:Class="SkinnedApplication.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:SkinnedApplication"
             StartupUri="MainWindow.xaml"
             Startup="App_Startup">
    <Application.Resources>
         
    </Application.Resources>
</Application>

3、App.xaml.cs添加 对应方法,加载两个皮肤;

private void App_Startup(object sender, StartupEventArgs e)
        {
            Properties["Blue"] = (ResourceDictionary)LoadComponent(new Uri("BlueSkin.xaml", UriKind.Relative));
            Properties["Yellow"] = (ResourceDictionary)LoadComponent(new Uri("YellowSkin.xaml", UriKind.Relative));

            // Note: you can also use the following syntax:
            //   Skins["Yellow"] = new YellowSkin()
            // But only as long as you implement the ResourceDictionary using markup and code-behind,
            // use the x:Class attribute in markup, and call InitializeComponent() from code-behind, eg:
            //
            //   <!-- Markup -->
            //   <ResourceDictionary
            //     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            //     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            //     xmlns:local="clr-namespace:SDKSample" 
            //     x:Class="SDKSample.YellowSkin">
            //        ...
            //   </ResourceDictionary>
            //
            //   // Code-behind
            //   public partial class YellowSkin : ResourceDictionary
            //   {
            //     public YellowSkin() { InitializeComponent(); }
            //   }
        }

4、 MainWindow.xaml.cs中添加如下初始化代码;

    //set initial skin
            Application.Current.Resources = (ResourceDictionary)Application.Current.Properties["Blue"];

5、如果用下拉框选择,在事件中添加如下代码;

    //Change the skin
            var selectedValue = (string)e.AddedItems[0];
            Application.Current.Resources = (ResourceDictionary)Application.Current.Properties[selectedValue];

maojie

目录
相关文章
|
C# 前端开发
WPF 小技巧
原文:WPF 小技巧 在使用mvvm模式开发时,对于Command的绑定是一件很伤脑筋的事情,尽管有强大的Blend类库支持: xmlns:Custom="http://www.galasoft.ch/mvvmlight"xmlns:i="http://schemas.
765 0
|
测试技术 C#
WPF DesiredSize & RenderSize
原文:WPF DesiredSize & RenderSize DesiredSize DesiredSize介绍 关于DesiredSize的介绍,可以查看最新微软文档对DesiredSize的介绍 DesiredSize,指的是元素在布局过程中计算所需要的大小。
1006 0
|
C#
浅谈WPF中的PreviewTextInput
原文:浅谈WPF中的PreviewTextInput     今天在使用TextBox的TextInput事件的时候,发现无论如何都不能触发该事件,然后百思不得其解,最后在MSDN上找到了答案:TextInput 事件可能已被标记为由复合控件的内部实现进行处理。
1174 0
|
算法 C#
WPF 实现水纹效果
原文:WPF 实现水纹效果 鼠标滑过产生水纹,效果图如下:     XMAL就放置了一个img标签   后台主要代码 窗体加载: private void Window_Loaded(object s...
1534 0
|
C#
WPF党旗和国徽!
原文:WPF党旗和国徽! 版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/yangyisen0713/article/details/18087007 ...
638 0
|
C# 测试技术
[WPF] PerformClick ?
原文:[WPF] PerformClick ?                                       [WPF] PerformClick ?                                                   周银辉   WPF没有提供这个方法,还真是让人觉得有些讨厌啊。
1061 0
|
C# Windows
WPF Adorner
原文:WPF Adorner 之前做项目时,为了实现类似微信消息数目的效果   image.png   ,我之前是修改的ControlTemplate。类似于将一个带数字的控件,放在另一个控件的右上角,来实现的这个效果。
1206 0
|
C# 索引
WPF MeshGeometry3D
原文:WPF MeshGeometry3D 说说 MeshGeometry3D 里 常用的 四个属性。 先看看 MSDN 的 简介 先说说 Positions,介绍说 是顶点位置的集合,什么意思,看张图片。
930 0
|
C#
在WPF中快速实现键盘钩子
原文:在WPF中快速实现键盘钩子 大部分的时候,当我们需要键盘事件的时候,可以通过在主窗口注册KeyBinding来实现,不过,有的时候我们需要的是全局键盘事件,想在任何一个地方都能使用,最开始的时候我是通过键盘钩子来实现的, 不过键盘钩子这种DLL调用的方式怎么都看着不大爽,这里介绍一种通过EventManager快速实现键盘事件感知的例子。
1076 0
|
C# Windows 异构计算
WPF(一)
什么是WPF   WPF(Windows Presentation Foundation)是用于Windows的现代图形显示系统。与之前出现的技术相比,WPF发生了根本性变化。WPF引用了"内置硬件加速"和"分辨率无关"等创新功能   WPF的底层图形技术使用的DirectX,而不再是古老的GDI/GGDI++。
1236 0