첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.

728x90
반응형
728x170

TestProject.zip
0.01MB

▶ BlueColor.xaml

<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>

 

728x90

 

▶ BlueSize.xaml

<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

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="BlueColor.xaml" />
        <ResourceDictionary Source="BlueSize.xaml"  />
    </ResourceDictionary.MergedDictionaries>    
</ResourceDictionary>

 

300x250

 

▶ RedColor.xaml

<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

<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

<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

<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

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
     <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="BorderStyle.xaml" />
    </ResourceDictionary.MergedDictionaries>   
</ResourceDictionary>

 

▶ Skin.cs

namespace TestProject
{
    /// <summary>
    /// 스킨
    /// </summary>
    public enum Skin
    {
        /// <summary>
        /// 적색
        /// </summary>
        Red,
        
        /// <summary>
        /// 청색
        /// </summary>
        Blue
    }
}

 

▶ MainApplication.xaml

<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

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("RESOURCE/BlueSkin.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
    }
}

 

▶ MainWindow.xaml

<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

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
    }
}
728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요