WPF皮肤制作思路

1.写了一个简单的换肤功能,暂时先记下,留着以后进行扩展。

界面定义

View Code
<Window x:Class="WpfApplication6.Window1"
    xmlns
="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x
="http://schemas.microsoft.com/winfx/2006/xaml"
    Title
="Window1" Height="300" Width="300">
    
<Grid>
        
<StackPanel>
            
<StackPanel Orientation="Horizontal">
                
<Button Content="红色" Margin="5" Click="Button_Click"/>
                
<Button Content="绿色" Margin="5" Click="Button_Click_1"/>
            
</StackPanel>
            
<Border Background="{DynamicResource LabelThinkness}" Height="50" Margin="5"/>
        
</StackPanel>
       
    
</Grid>
</Window>

后台界面

View Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApplication6
{
    
public partial class Window1 : Window
    {
        
public Window1()
        {
            InitializeComponent();
        }

        
private void Button_Click(object sender, RoutedEventArgs e)
        {
            Application.Current.Resources.MergedDictionaries[
0= new ResourceDictionary() { Source = new Uri("RedCorlor.xaml", UriKind.Relative) };
        }

        
private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            Application.Current.Resources.MergedDictionaries[
0= new ResourceDictionary() { Source = new Uri("GreenCorlor.xaml", UriKind.Relative) };
        }
    }
}

RedCorlor.xaml

View Code
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x
="http://schemas.microsoft.com/winfx/2006/xaml">
    
<SolidColorBrush Color="Green" x:Key="LabelThinkness"/>
</ResourceDictionary>

GreenCorlor.xaml

View Code
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x
="http://schemas.microsoft.com/winfx/2006/xaml">
    
<SolidColorBrush Color="Red" x:Key="LabelThinkness"/>
</ResourceDictionary>

APP.XAML

View Code
<Application x:Class="WpfApplication6.App"
    xmlns
="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x
="http://schemas.microsoft.com/winfx/2006/xaml"
    StartupUri
="Window1.xaml">
    
<Application.Resources>
        
<ResourceDictionary x:Name="AppResource">
            
<ResourceDictionary.MergedDictionaries>
                
<ResourceDictionary Source="GreenCorlor.xaml"/>
            
</ResourceDictionary.MergedDictionaries>
        
</ResourceDictionary>
    
</Application.Resources>
</Application>
其实这个方法,不是特别好。是我随便写的。也不知道 WPF最佳换肤应该怎么来写。希望能给予建议和支持。
原文地址:https://www.cnblogs.com/tongly/p/2022415.html