■ ResourceDictionary 클래스를 사용해 개별 어셈블리의 리소스 딕셔너리를 만드는 방법을 보여준다.
[TestLibrary 프로젝트]
▶ 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> |
▶ 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> |
[TestProject 프로젝트]
▶ 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 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="/TestLibrary;component/RESOURCE/RedSkin.xaml" /> <ResourceDictionary Source="/TestLibrary;component/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 |
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) { AddResource("/TestLibrary;Component/RESOURCE/BlueSkin.xaml"); } else if(Skin == Skin.Red) { AddResource("/TestLibrary;Component/RESOURCE/RedSkin.xaml"); } AddResource("/TestLibrary;Component/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 } } |
▶ 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 } } |