첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
------------------------------------------------------------------------------------------------------------------------------------------------------
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>

 

 

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

 

300x250

 

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

 

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

 

▶ Skin.cs

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

 

▶ SkinResourceDictionary.cs

using System;
using System.Windows;

namespace TestProject
{
    /// <summary>
    /// 스킨 리소스 딕셔너리
    /// </summary>
    public class SkinResourceDictionary : ResourceDictionary
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Field
        ////////////////////////////////////////////////////////////////////////////////////////// Private

        #region Field

        /// <summary>
        /// 적색 스킨 URI
        /// </summary>
        private Uri redSkinURI;

        /// <summary>
        /// 청색 스킨 URI
        /// </summary>
        private Uri blueSkinURI;

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Property
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 적색 스킨 URI - RedSkinURI

        /// <summary>
        /// 적색 스킨 URI
        /// </summary>
        public Uri RedSkinURI
        {
            get
            {
                return this.redSkinURI;
            }
            set
            {
                this.redSkinURI = value;

                UpdateSource();
            }
        }

        #endregion
        #region 청색 스킨 URI - BlueSkinURI

        /// <summary>
        /// 청색 스킨 URI
        /// </summary>
        public Uri BlueSkinURI
        {
            get
            {
                return this.blueSkinURI;
            }
            set
            {
                this.blueSkinURI = value;
        
                UpdateSource();
            }
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Private

        #region 소스 업데이트하기 - UpdateSource()

        /// <summary>
        /// 소스 업데이트하기
        /// </summary>
        private void UpdateSource()
        {
            Uri uri = MainApplication.Skin == Skin.Red ? RedSkinURI : BlueSkinURI;

            if(uri != null && base.Source != uri)
            {
                base.Source = uri;
            }
        }

        #endregion
    }
}

 

▶ 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"
    xmlns:local="clr-namespace:TestProject"
    StartupUri="MainWindow.xaml">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <local:SkinResourceDictionary
                    RedSkinURI="RESOURCE/RedColor.xaml" 
                    BlueSkinURI="RESOURCE/BlueColor.xaml" />
                <local:SkinResourceDictionary
                    RedSkinURI="RESOURCE/RedSize.xaml" 
                    BlueSkinURI="RESOURCE/BlueSize.xaml" />
                <ResourceDictionary Source="RESOURCE/BorderStyle.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

 

▶ MainApplication.xaml.cs

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

 

▶ 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>
        <Border
            Style="{StaticResource BorderStyleKey}"
            Width="300"
            Height="300" />
    </Grid>
</Window>
728x90
그리드형(광고전용)
Posted by icodebroker
,