WPF-皮肤制作思路

本文介绍了一种在WPF应用程序中实现界面换肤的方法,通过动态资源更改背景颜色,展示了红色和绿色主题的切换代码示例。

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

界面定义

 

ExpandedBlockStart.gifView 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>

后台界面

 

ExpandedBlockStart.gifView 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

 

ExpandedBlockStart.gifView 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

 

ExpandedBlockStart.gifView 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

 

ExpandedBlockStart.gifView 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/archive/2011/04/20/2022415.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值