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

728x90
반응형
728x170

TestSolution.zip
0.01MB

[TestLibrary 프로젝트]

 

▶ ResourceDictionary.xaml

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

 

728x90

 

[TestProject 프로젝트]

 

▶ ResourceDictionary.xaml

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

 

300x250

 

▶ BAMLReader.cs

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

<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="BAML 스트림에서 리소스 구하기"
    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 프로젝트&#10;ResourceDictionary.xaml&#10;RadialGrandientBrush" />
        <Rectangle Name="leftRectangle" Grid.Row="3" Grid.Column="1"
            Width="200"
            Height="200" />
        <TextBlock Grid.Row="1" Grid.Column="3"
            Text="TestProject 프로젝트&#10;ResourceDictionary.xaml&#10;LinearGrandientBrush" />
        <Rectangle Name="rightRectangle" Grid.Row="3" Grid.Column="3"
            Width="200"
            Height="200" />
    </Grid>
</Window>

 

▶ MainWindow.xaml.cs

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

댓글을 달아 주세요