첨부 소스 코드는 나눔고딕코딩 폰트를 사용합니다.
728x90
반응형
728x170

TestProject.zip
0.83MB

▶ ImageResourceExtension.cs

using System.Reflection;
using System.Xml;

namespace TestProject;

/// <summary>
/// 이미지 리소스 확장
/// </summary>
[ContentProperty("Source")]
public class ImageResourceExtension : IMarkupExtension<ImageSource>
{
    //////////////////////////////////////////////////////////////////////////////////////////////////// Property
    ////////////////////////////////////////////////////////////////////////////////////////// Public

    #region 소스 - Source

    /// <summary>
    /// 소스
    /// </summary>
    public string Source { set; get; }

    #endregion

    //////////////////////////////////////////////////////////////////////////////////////////////////// Method
    ////////////////////////////////////////////////////////////////////////////////////////// Public

    #region 값 제공하기 - ProvideValue(serviceProvider)

    /// <summary>
    /// 값 제공하기
    /// </summary>
    /// <param name="serviceProvider">서비스 제공자</param>
    /// <returns>이미지 소스</returns>
    /// <exception cref="XamlParseException"></exception>
    public ImageSource ProvideValue(IServiceProvider serviceProvider)
    {
        if(string.IsNullOrEmpty(Source))
        {
            IXmlLineInfoProvider lineInfoProvider = serviceProvider.GetService(typeof(IXmlLineInfoProvider)) as IXmlLineInfoProvider;

            IXmlLineInfo lineInfo = (lineInfoProvider != null) ? lineInfoProvider.XmlLineInfo : new XmlLineInfo();

            throw new XamlParseException("ImageResourceExtension requires Source property to be set", lineInfo);
        }

        string assemblyName = GetType().GetTypeInfo().Assembly.GetName().Name;

        return ImageSource.FromResource($"{assemblyName}.{Source}", typeof(ImageResourceExtension).GetTypeInfo().Assembly);
    }

    #endregion
    #region 값 제공하기 - IMarkupExtension.ProvideValue(serviceProvider)

    /// <summary>
    /// 값 제공하기
    /// </summary>
    /// <param name="serviceProvider">서비스 제공자</param>
    /// <returns>마크업 확장 인터페이스</returns>
    object IMarkupExtension.ProvideValue(IServiceProvider serviceProvider)
    {
        return (this as IMarkupExtension<ImageSource>).ProvideValue(serviceProvider);
    }

    #endregion
}

 

728x90

 

▶ MainPage.xaml

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage x:Class="TestProject.MainPage"
    xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:local="clr-namespace:TestProject">
    <Image
        WidthRequest="300"
        Source="{local:ImageResource IMAGE.sample.jpg}" />
</ContentPage>
728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요