■ ResourceDictionary 클래스를 사용해 리소스에서 리소스 딕셔너리를 구하는 방법을 보여준다.
[TestLibrary 프로젝트]
▶ ResourceDictionary.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"> <RadialGradientBrush x:Key="RadialGradientBrushKey"> <RadialGradientBrush.GradientStops> <GradientStop Color="Pink" Offset="0" /> <GradientStop Color="Aqua" Offset="1" /> </RadialGradientBrush.GradientStops> </RadialGradientBrush> </ResourceDictionary> |
[TestProject 프로젝트]
▶ ResourceDictionary.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"> <LinearGradientBrush.GradientStops> <GradientStop Color="Pink" Offset="0" /> <GradientStop Color="Aqua" Offset="1" /> </LinearGradientBrush.GradientStops> </LinearGradientBrush> </ResourceDictionary> |
▶ BAMLReader.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 |
using System; using System.IO; using System.Reflection; using System.Windows; using System.Windows.Markup; using System.Windows.Resources; namespace TestProject { /// <summary> /// BAML 리더 /// </summary> public static class BAMLReader { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region 리소스 스트림 구하기 - GetResourceStream(assemblyName, resourcePath) /// <summary> /// 리소스 스트림 구하기 /// </summary> /// <param name="assemblyName">어셈블리명</param> /// <param name="resourcePath">리소스 경로</param> /// <returns>리소스 스트림</returns> public static Stream GetResourceStream(string assemblyName, string resourcePath) { Uri uri = new Uri($"/{assemblyName};component/{resourcePath}", UriKind.Relative); StreamResourceInfo streamResourceInfo = Application.GetResourceStream(uri); return streamResourceInfo.Stream; } #endregion #region 로드하기 - Load(stream) /// <summary> /// 로드하기 /// </summary> /// <param name="stream">스트림</param> /// <returns>객체</returns> public static object Load(Stream stream) { ParserContext context = new ParserContext(); MethodInfo methodInfo = typeof(XamlReader).GetMethod("LoadBaml", BindingFlags.NonPublic | BindingFlags.Static); return methodInfo.Invoke(null, new object[] { stream, context, null, false }); } #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 36 37 |
<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 Margin="10"> <Grid.ColumnDefinitions> <ColumnDefinition Width="*" /> <ColumnDefinition Width="Auto" /> <ColumnDefinition Width="*" /> <ColumnDefinition Width="Auto" /> <ColumnDefinition Width="*" /> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition Height="*" /> <RowDefinition Height="Auto" /> <RowDefinition Height="10" /> <RowDefinition Height="Auto" /> <RowDefinition Height="*" /> </Grid.RowDefinitions> <TextBlock Grid.Row="1" Grid.Column="1" Text="TestLibrary 프로젝트 ResourceDictionary.xaml RadialGrandientBrush" /> <Rectangle Name="leftRectangle" Grid.Row="3" Grid.Column="1" Width="200" Height="200" /> <TextBlock Grid.Row="1" Grid.Column="3" Text="TestProject 프로젝트 ResourceDictionary.xaml LinearGrandientBrush" /> <Rectangle Name="rightRectangle" Grid.Row="3" Grid.Column="3" Width="200" Height="200" /> </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 |
using System.IO; 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(); Stream leftStream = BAMLReader.GetResourceStream("TestLibrary", "ResourceDictionary.xaml"); ResourceDictionary leftDictionary = BAMLReader.Load(leftStream) as ResourceDictionary; RadialGradientBrush leftBrush = leftDictionary["RadialGradientBrushKey"] as RadialGradientBrush; this.leftRectangle.Fill = leftBrush; Stream rightStream = BAMLReader.GetResourceStream("TestProject", "ResourceDictionary.xaml"); ResourceDictionary rightDictionary = BAMLReader.Load(rightStream) as ResourceDictionary; LinearGradientBrush rightBrush = rightDictionary["LinearGradientBrushKey"] as LinearGradientBrush; this.rightRectangle.Fill = rightBrush; } #endregion } } |