XAML 在.NET Maui中着色svg图像的颜色

aoyhnmkz  于 2023-09-28  发布在  .NET
关注(0)|答案(1)|浏览(161)

当我从Xamarin.Forms转换时,我试图在.NET Maui中动态地为svg图像着色。我设置了下面的代码来使用字符串格式的颜色绑定,但是我得到了下面的错误。如何在.NET Maui中实现这一点?
错误XLS0503无法将“IconTintColorBehavior”类型的值添加到“IList”类型的集合或字典“1”中
XAML代码

<Grid>
    <Image
    x:Name="NeedleSVG"
    WidthRequest="200"
    HeightRequest="200"
    Source="resource://XamarinAttributeProgrammer.Resources.Images.Needle.svg">
        <Image.Effects>
            <mct:IconTintColorBehavior TintColor="{Binding NeedleColorInHex}" />
        </Image.Effects>
    </Image>

    <Image
    WidthRequest="200"
    HeightRequest="200"
    Source="resource://XamarinAttributeProgrammer.Resources.Images.Backlight.svg">
        <Image.Effects>
            <mct:IconTintColorBehavior TintColor="{Binding BackColorInHex}" />
        </Image.Effects>
    </Image>

    <Image
    WidthRequest="200"
    HeightRequest="200"
    Source="resource://XamarinAttributeProgrammer.Resources.Images.Housing.svg">
        <Image.Effects>
            <mct:IconTintColorBehavior TintColor="{Binding BackColorInHex}" />
        </Image.Effects>
    </Image>
    <Button ClassId="BackLight" 
            BackgroundColor="Transparent" 
            WidthRequest="250" 
            HorizontalOptions="Center" 
            Clicked="OnPreviewTapped"/>
    <Button ClassId="Needle" 
            BackgroundColor="Transparent" 
            HeightRequest="80" 
            WidthRequest="80" 
            VerticalOptions="Center" 
            HorizontalOptions="Center" 
            Margin="0,20,0,0" 
            Clicked="OnPreviewTapped"/>
</Grid>

视图模型

public string SelectedColorInHex
{
    get
    {
        NotifyGaugeOfChangeAsync();
        Task.Delay(50).Wait(); // wait 50ms so that we dont flood the gauge
        if (FocusColorOn == BACKLIGHT)
            return BackColorInHex;
        else
            return NeedleColorInHex;
    }
    set
    {
        if (FocusColorOn == BACKLIGHT)
            _colorBack.Hex = value;
        else
            _colorNeedle.Hex = value;
    }
}
kqlmhetl

kqlmhetl1#

你正在尝试添加一个行为效果集合

<Image.Effects>
    <mct:IconTintColorBehavior TintColor="{Binding NeedleColorInHex}" />
</Image.Effects>

<Image.Behaviors>
    <mct:IconTintColorBehavior TintColor="{Binding NeedleColorInHex}" />
</Image.Behaviors>

相关问题