■ Application 클래스의 FindResource 메소드를 사용해 애플리케이션 범위의 리소스를 구하는 방법을 보여준다.
▶ MainResourceDictionary.xaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <SolidColorBrush x:Key="StandardSolidColorBrushKey" Color="Blue" /> <LinearGradientBrush x:Key="StandardLinearGradientBrushKey" StartPoint="0.0 0.0" EndPoint="1.0 1.0"> <LinearGradientBrush.GradientStops> <GradientStop Color="White" Offset="0" /> <GradientStop Color="Black" Offset="1" /> </LinearGradientBrush.GradientStops> </LinearGradientBrush> </ResourceDictionary> |
▶ MainApplication.xaml
1 2 3 4 5 6 7 8 9 10 |
<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 Source="MainResourceDictionary.xaml" /> </Application.Resources> </Application> |
▶ MainWindow.xaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<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="Application 클래스 : FindResource 메소드를 사용해 애플리케이션 범위의 리소스 구하기" FontFamily="나눔고딕코딩" FontSize="16"> <Rectangle Name="rectangle" Width="200" Height="200" /> </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(); Brush brush = (Brush)Application.Current.FindResource("StandardLinearGradientBrushKey"); this.rectangle.Fill = brush; } #endregion } } |