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

目录
相关文章
|
6月前
|
存储 C# 索引
WPF/C#:BusinessLayerValidation
WPF/C#:BusinessLayerValidation
46 0
|
C# Windows
WPF WriteableBitmap的使用
选中项目按右键添加-资源文件-WPF-窗口generate_bitmap.xml文件内容为
2167 0
|
C# 图形学 数据格式
WPF中的3D Wireframe
原文:WPF中的3D Wireframe WPF不支持画三维线,但开发人员提供了ScreenSpaceLines3D 类用于实现这个功能。
1335 0
|
数据建模 C#
WPF InkCanvas 毛笔效果
原文:WPF InkCanvas 毛笔效果 1、先来看看InkCanvas的一般用法:                                                                                 2、自定义InkCanvas,实现毛笔效果...
1278 0
|
C#
使用WPF实现3D场景[二]
原文:使用WPF实现3D场景[二] 在上一篇的文章里我们知道如何构造一个简单的三维场景,这次的课程我将和大家一起来研究如何用代码,完成对建立好了的三维场景的观察。
1163 0
|
C#
使用WPF实现3D场景[一]
原文:使用WPF实现3D场景[一] 在这篇文章里,将介绍如何实现一个简单的三维场景,一个三维的空间,包括空间内的三维物体的组合. 首先介绍一下一个三维场景里的基本元素: 先是定义一个简单的三维的场景环境 代码如下: 以上是定义了一个名称叫做 myViewport 的的三维场景,接下来可以在这个三位场景里添加一些元素: 元素一:照相机 照相机是三维场景内用户的视角,当然照相机也是唯一的。
1639 0
|
测试技术 C#
WPF DesiredSize & RenderSize
原文:WPF DesiredSize & RenderSize DesiredSize DesiredSize介绍 关于DesiredSize的介绍,可以查看最新微软文档对DesiredSize的介绍 DesiredSize,指的是元素在布局过程中计算所需要的大小。
1031 0
|
C#
浅谈WPF中的PreviewTextInput
原文:浅谈WPF中的PreviewTextInput     今天在使用TextBox的TextInput事件的时候,发现无论如何都不能触发该事件,然后百思不得其解,最后在MSDN上找到了答案:TextInput 事件可能已被标记为由复合控件的内部实现进行处理。
1214 0
|
C#
浅谈WPF的VisualBrush
原文:浅谈WPF的VisualBrush     首先看看VisualBrush的解释,msdn上面的解释是使用 Visual 绘制区域,那么我们再来看看什么是Visual呢?官方的解释是:获取或设置画笔的内容,Visual 是直接继承自DependencyObject,UIElement也是直接继...
2229 0
|
C# 测试技术
[WPF] PerformClick ?
原文:[WPF] PerformClick ?                                       [WPF] PerformClick ?                                                   周银辉   WPF没有提供这个方法,还真是让人觉得有些讨厌啊。
1086 0