ImageResourceConverter

ImageResourceConverter 是一个转换器,用于将嵌入图像资源 ID 转换为其 ImageSource。 嵌入图像资源是指将图像添加到项目中,并将“生成操作”设置为“嵌入资源”。 它的 ID 是完全限定的名称;因此是项目的命名空间+资源名称。 例如,对于名为 CommunityToolkit.Maui.Sample 的项目、一组 Resources/Embedded 的嵌套文件夹和名为 dotnetbot.png 的图像,将采用如下方式生成 ID:

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

这将产生:

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

BaseConverter 属性

以下属性是在基类 public abstract class BaseConverter 中实现的:

properties 说明
DefaultConvertReturnValue IValueConverter.Convert(object?, Type, object?, CultureInfo?) 引发 Exception 时返回的默认值。 当 CommunityToolkit.Maui.Options.ShouldSuppressExceptionsInConverters 设置为 true 时,将使用此值。
DefaultConvertBackReturnValue IValueConverter.ConvertBack(object?, Type, object?, CultureInfo?) 引发 Exception 时返回的默认值。 当 CommunityToolkit.Maui.Options.ShouldSuppressExceptionsInConverters 设置为 true 时,将使用此值。

ICommunityToolkitValueConverter 属性

以下属性在 public interface ICommunityToolkitValueConverter 中实现:

properties 类型​​ 描述
DefaultConvertReturnValue object? IValueConverter.Convert(object?, Type, object?, CultureInfo?) 引发 Exception 时返回的默认值。 当 CommunityToolkit.Maui.Options.ShouldSuppressExceptionsInConverters 设置为 true 时,将使用此值。
DefaultConvertBackReturnValue object? 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

ImageResourceConverter 可以在 XAML 中按如下所示方式使用:

<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(
                static (ViewModel vm) => vm.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 的源代码