■ ResourceDictionary 클래스에서 컴파일 및 동적 스키닝을 사용하는 방법을 보여준다.
▶ BlueColor.xaml
1 2 3 4 5 6 7 |
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <SolidColorBrush x:Key="NormalBackgroundSolidColorBrushKey" Color="Blue" /> </ResourceDictionary> |
▶ BlueSize.xaml
1 2 3 4 5 6 7 |
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <CornerRadius x:Key="NormalCornerRadiusKey">0</CornerRadius> </ResourceDictionary> |
▶ RedColor.xaml
1 2 3 4 5 6 7 |
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <SolidColorBrush x:Key="NormalBackgroundSolidColorBrushKey" Color="Red" /> </ResourceDictionary> |
▶ RedSize.xaml
1 2 3 4 5 6 7 |
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <CornerRadius x:Key="NormalCornerRadiusKey">10</CornerRadius> </ResourceDictionary> |
▶ BorderStyle.xaml
1 2 3 4 5 6 7 8 9 10 |
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <Style x:Key="BorderStyleKey" TargetType="Border" > <Setter Property="Background" Value="{StaticResource NormalBackgroundSolidColorBrushKey}" /> <Setter Property="CornerRadius" Value="{StaticResource NormalCornerRadiusKey}" /> </Style> </ResourceDictionary> |
▶ Skin.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
namespace TestProject { /// <summary> /// 스킨 /// </summary> public enum Skin { /// <summary> /// 적색 /// </summary> Red, /// <summary> /// 청색 /// </summary> Blue } } |
▶ MainApplication.xaml
1 2 3 4 5 6 7 |
<Application x:Class="TestProject.MainApplication" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" StartupUri="MainWindow.xaml"> </Application> |
▶ MainApplication.xaml.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
using System; using System.Windows; namespace TestProject { /// <summary> /// 메인 애플리케이션 /// </summary> public partial class MainApplication : Application { //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region 스킨 - Skin /// <summary> /// 스킨 /// </summary> public static Skin Skin { get; set; } = Skin.Red; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Instance //////////////////////////////////////////////////////////////////////////////// Public #region 스킨 변경하기 - ChangeSkin(Skin skin) /// <summary> /// 스킨 변경하기 /// </summary> /// <param name="skin">스킨</param> public void ChangeSkin(Skin skin) { Skin = skin; Resources.Clear(); Resources.MergedDictionaries.Clear(); if(Skin == Skin.Blue) { AddBlueSkin(); } else if(Skin == Skin.Red) { AddRedSkin(); } AddBorderStyle(); } #endregion //////////////////////////////////////////////////////////////////////////////// Protected #region 시작시 처리하기 - OnStartup(e) /// <summary> /// 시작시 처리하기 /// </summary> /// <param name="e">이벤트 인자</param> protected override void OnStartup(StartupEventArgs e) { base.OnStartup(e); ChangeSkin(Skin.Red); } #endregion //////////////////////////////////////////////////////////////////////////////// Private #region 리소스 딕셔너리 추가하기 - AddResourceDictionary(uri) /// <summary> /// 리소스 딕셔너리 추가하기 /// </summary> /// <param name="uri">URI</param> private void AddResourceDictionary(string uri) { Resources.MergedDictionaries.Add ( new ResourceDictionary() { Source = new Uri(uri, UriKind.Relative) } ); } #endregion #region 청색 스킨 추가하기 - AddBlueSkin() /// <summary> /// 청색 스킨 적용하기 /// </summary> private void AddBlueSkin() { AddResourceDictionary("RESOURCE/BlueColor.xaml"); AddResourceDictionary("RESOURCE/BlueSize.xaml" ); } #endregion #region 적색 스킨 추가하기 - AddRedSkin() /// <summary> /// 적색 스킨 추가하기 /// </summary> private void AddRedSkin() { AddResourceDictionary("RESOURCE/RedColor.xaml"); AddResourceDictionary("RESOURCE/RedSize.xaml" ); } #endregion #region 테두리 스타일 추가하기 - AddBorderStyle() /// <summary> /// 테두리 스타일 추가하기 /// </summary> private void AddBorderStyle() { AddResourceDictionary("RESOURCE/BorderStyle.xaml"); } #endregion } } |
▶ MainWindow.xaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
<Window x:Class="TestProject.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Width="800" Height="600" Title="ResourceDictionary 클래스 : 컴파일 및 동적 스키닝 사용하기" FontFamily="나눔고딕코딩" FontSize="16"> <Grid> <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center"> <Border Name="border" Style="{DynamicResource BorderStyleKey}" Width="300" Height="300" /> <StackPanel HorizontalAlignment="Center" Margin="0 10 0 0" Orientation="Horizontal"> <Button Name="blueSkinButton" Width="100" Height="30" Content="청색 스킨" /> <Button Name="redSkinButton" Margin="10 0 0 0" Width="100" Height="30" Content="적색 스킨" /> </StackPanel> </StackPanel> </Grid> </Window> |
▶ MainWindow.xaml.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
using System.Windows; namespace TestProject { /// <summary> /// 메인 윈도우 /// </summary> public partial class MainWindow : Window { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainWindow() /// <summary> /// 생성자 /// </summary> public MainWindow() { InitializeComponent(); this.blueSkinButton.Click += blueSkinButton_Click; this.redSkinButton.Click += redSkinButton_Click; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private #region 청색 스킨 버튼 클릭시 처리하기 - blueSkinButton_Click(sender, e) /// <summary> /// 청색 스킨 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void blueSkinButton_Click(object sender, RoutedEventArgs e) { (Application.Current as MainApplication).ChangeSkin(Skin.Blue); } #endregion #region 적색 스킨 버튼 클릭시 처리하기 - redSkinButton_Click(sender, e) /// <summary> /// 적색 스킨 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void redSkinButton_Click(object sender, RoutedEventArgs e) { (Application.Current as MainApplication).ChangeSkin(Skin.Red); } #endregion } } |