■ ResourceDictionary 클래스에서 느슨한 XAML 스키닝을 사용하는 방법을 보여준다.
▶ 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> |
▶ BlueSkin.xaml
1 2 3 4 5 6 7 8 |
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="BlueColor.xaml" /> <ResourceDictionary Source="BlueSize.xaml" /> </ResourceDictionary.MergedDictionaries> </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> |
▶ RedSkin.xaml
1 2 3 4 5 6 7 8 |
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="RedColor.xaml" /> <ResourceDictionary Source="RedSize.xaml" /> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> |
▶ GreenSkin.xaml
1 2 3 4 5 6 7 8 |
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <SolidColorBrush x:Key="NormalBackgroundSolidColorBrushKey" Color="Green" /> <CornerRadius x:Key="NormalCornerRadiusKey">20</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> |
▶ BorderStyle.xaml
1 2 3 4 5 6 7 |
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="BorderStyle.xaml" /> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> |
▶ Skin.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 |
namespace TestProject { /// <summary> /// 스킨 /// </summary> public enum Skin { /// <summary> /// 적색 /// </summary> Red, /// <summary> /// 녹색 /// </summary> Green, /// <summary> /// 청색 /// </summary> Blue } } |
▶ MainApplication.xaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<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.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="RESOURCE/RedSkin.xaml" /> <ResourceDictionary Source="RESOURCE/SharedStyle.xaml" /> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </Application.Resources> </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 |
using System; using System.IO; using System.Windows; using System.Windows.Markup; 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) { AddResource("RESOURCE/BlueSkin.xaml"); } else if(Skin == Skin.Green) { AddLooseReource("RESOURCE/GreenSkin.xaml"); } else if(Skin == Skin.Red) { AddResource("RESOURCE/RedSkin.xaml"); } AddResource("RESOURCE/SharedStyle.xaml"); } #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 리소스 추가하기 - AddResource(uri) /// <summary> /// 리소스 추가하기 /// </summary> /// <param name="uri">URI</param> private void AddResource(string uri) { ResourceDictionary dictionary = new ResourceDictionary() { Source = new Uri(uri, UriKind.Relative) }; foreach(ResourceDictionary mergedDictionary in dictionary.MergedDictionaries) { Resources.MergedDictionaries.Add(mergedDictionary); } foreach(object key in dictionary.Keys) { Resources[key] = dictionary[key]; } } #endregion #region 느슨한 리소스 추가하기 - AddLooseReource(filePath) /// <summary> /// 느슨한 리소스 추가하기 /// </summary> /// <param name="filePath">파일 경로</param> private void AddLooseReource(string filePath) { StreamReader reader = new StreamReader(filePath); ResourceDictionary dictionary = XamlReader.Load(reader.BaseStream) as ResourceDictionary; Resources.MergedDictionaries.Add(dictionary); } #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 36 37 38 39 40 |
<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 클래스 : 느슨한 XAML 스키닝 사용하기" 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="greenSkinButton" Margin="10 0 0 0" 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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
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.greenSkinButton.Click += greenSkinButton_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 녹색 스킨 버튼 클릭시 처리하기 - greenSkinButton_Click(sender, e) /// <summary> /// 녹색 스킨 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void greenSkinButton_Click(object sender, RoutedEventArgs e) { (Application.Current as MainApplication).ChangeSkin(Skin.Green); } #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 } } |