■ Application 클래스의 LoadComponent 정적 메소드를 사용해 리소스 딕셔너리 참조를 구하는 방법을 보여준다.
▶ MainDictionary.xaml
1 2 3 4 5 6 7 8 9 10 11 12 |
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <LinearGradientBrush x:Key="LinearGradientBrushKey" StartPoint="0 0" EndPoint="1 1"> <GradientStop Offset="0.25" Color="Red" /> <GradientStop Offset="0.75" Color="Blue" /> </LinearGradientBrush> </ResourceDictionary> |
▶ DictionaryHelper.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 |
using System; using System.Windows; namespace TestProject { /// <summary> /// 딕셔너리 헬퍼 /// </summary> public static class DictionaryHelper { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 메인 딕셔너리 /// </summary> private static ResourceDictionary _mainDictionary; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region 메인 딕셔너리 - MainDictionary /// <summary> /// 메인 딕셔너리 /// </summary> public static ResourceDictionary MainDictionary { get { if (_mainDictionary == null) { Uri uri = new Uri("/TestProject;component/MainDictionary.xaml", UriKind.Relative); _mainDictionary = (ResourceDictionary)Application.LoadComponent(uri); } return _mainDictionary; } } #endregion } } |
▶ MainWindow.xaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<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="TestProject" FontFamily="나눔고딕코딩" FontSize="16"> <Ellipse Name="ellipse" Width="300" Height="300" StrokeThickness="3" Stroke="Black" /> </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 |
using System.Windows; using System.Windows.Media; namespace TestProject { /// <summary> /// 메인 윈도우 /// </summary> public partial class MainWindow : Window { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainWindow() /// <summary> /// 생성자 /// </summary> public MainWindow() { InitializeComponent(); LinearGradientBrush brush = DictionaryHelper.MainDictionary["LinearGradientBrushKey"] as LinearGradientBrush; this.ellipse.Fill = brush; } #endregion } } |