Share via


ImageResourceConverter

포함된 ImageResourceConverter 이미지 리소스 ID를 ImageSource로 변환하는 변환기입니다. 포함된 이미지 리소스는 빌드 작업이 포함된 리소스로 설정된 이미지가 프로젝트에 추가된 경우입니다. ID는 정규화된 이름입니다. 프로젝트의 네임스페이스 + 리소스 이름입니다. 프로젝트 CommunityToolkit.Maui.Sample예제에서 중첩된 폴더 Resources/Embedded 집합과 ID라는 dotnetbot.png 이미지가 다음과 함께 생성됩니다.

CommunityToolkit.Maui.Sample + Resources.Embedded + dotnetbot.png

결과는 다음과 같습니다.

CommunityToolkit.Maui.Sample.Resources.Embedded.dotnetbot.png

BaseConverter 속성

다음 속성은 기본 클래스 public abstract class BaseConverter에서 구현됩니다.

속성 설명
DefaultConvertReturnValue 을 throw할 때 IValueConverter.Convert(object?, Type, object?, CultureInfo?) 반환할 기본값입니다 Exception. 이 값은 CommunityToolkit.Maui.Options.ShouldSuppressExceptionsInConverters가 로 설정된 true경우에 사용됩니다.
DefaultConvertBackReturnValue 을 throw할 때 IValueConverter.ConvertBack(object?, Type, object?, CultureInfo?) 반환할 기본값입니다 Exception. 이 값은 CommunityToolkit.Maui.Options.ShouldSuppressExceptionsInConverters가 로 설정된 true경우에 사용됩니다.

ICommunityToolkitValueConverter 속성

다음 속성은 다음에서 구현됩니다.public interface ICommunityToolkitValueConverter

속성 Type 설명
DefaultConvertReturnValue object? 을 throw할 때 IValueConverter.Convert(object?, Type, object?, CultureInfo?) 반환할 기본값입니다 Exception. 이 값은 CommunityToolkit.Maui.Options.ShouldSuppressExceptionsInConverters가 로 설정된 true경우에 사용됩니다.
DefaultConvertBackReturnValue object? 을 throw할 때 IValueConverter.ConvertBack(object?, Type, object?, CultureInfo?) 반환할 기본값입니다 Exception. 이 값은 CommunityToolkit.Maui.Options.ShouldSuppressExceptionsInConverters가 로 설정된 true경우에 사용됩니다.

구문

XAML

XAML 네임스페이스 포함

XAML에서 도구 키트를 사용하려면 페이지 또는 보기에 다음 xmlns 을 추가해야 합니다.

xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"

따라서 다음을 수행합니다.

<ContentPage
    x:Class="CommunityToolkit.Maui.Sample.Pages.MyPage"
    xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml">

</ContentPage>

다음과 같이 포함 xmlns 하도록 수정됩니다.

<ContentPage
    x:Class="CommunityToolkit.Maui.Sample.Pages.MyPage"
    xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit">

</ContentPage>

ImageResourceConverter 사용

XAML ImageResourceConverter 에서 다음과 같이 사용할 수 있습니다.

<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"
             x:Class="CommunityToolkit.Maui.Sample.Pages.Converters.ImageResourceConverterPage">

    <ContentPage.Resources>
        <ResourceDictionary>
            <toolkit:ImageResourceConverter x:Key="ImageResourceConverter" />
        </ResourceDictionary>
    </ContentPage.Resources>

    <Image Source="{Binding MyImageResource, Converter={StaticResource ImageResourceConverter}}" />

</ContentPage>

C#

ImageResourceConverter C#에서 다음과 같이 사용할 수 있습니다.

class ImageResourceConverterPage : ContentPage
{
    public ImageResourceConverterPage()
    {
        var image = new Image();

        image.SetBinding(
            Image.SourceProperty, 
            new Binding(
                nameof(ViewModel.MyImageResource), 
                converter: new ImageResourceConverter())); 

        Content = label;        
    }
}

C# 태그

CommunityToolkit.Maui.Markup 패키지는 C#에서 이 변환기를 사용하는 훨씬 더 간결한 방법을 제공합니다.

using CommunityToolkit.Maui.Markup;

class ImageResourceConverterPage : ContentPage
{
    public ImageResourceConverterPage()
    {
        Content = new Image()
            .Bind(
                Label.SourceProperty, 
                static (ViewModel vm) => vm.MyImageResource,
                converter: new ImageResourceConverter()); 
    }
}

예제

.NET MAUI 커뮤니티 도구 키트 샘플 애플리케이션에서 작동 중인 이 변환기의 예를 찾을 수 있습니다.

API

.NET MAUI 커뮤니티 도구 키트 GitHub 리포지토리에서 오버에 대한 ImageResourceConverter 소스 코드를 찾을 수 있습니다.