着色颜色不适用于iOS中Xamarin格式的图像?

着色颜色不适用于iOS中Xamarin格式的图像?,ios,image,xamarin.forms,cross-platform,tintcolor,Ios,Image,Xamarin.forms,Cross Platform,Tintcolor,我正在使用with nuget软件包处理xamarin表单中的嵌入图像着色,在android中效果很好,但在iOS中效果不好。 根据我的搜索,我一直使用渲染模式进行示例,但在我的场景中,我无法使用渲染模式。 我所做的是使用解决方案文件中的图像引用,而不是iOS资产中的图像引用 下面是我的代码片段 <controls:TintedImage x:Name="ColorChange" Aspect="AspectFit

我正在使用with nuget软件包处理xamarin表单中的嵌入图像着色,在android中效果很好,但在iOS中效果不好。 根据我的搜索,我一直使用渲染模式进行示例,但在我的场景中,我无法使用渲染模式。 我所做的是使用解决方案文件中的图像引用,而不是iOS资产中的图像引用

下面是我的代码片段

 <controls:TintedImage
           x:Name="ColorChange"
           Aspect="AspectFit"
           HeightRequest="17"
           Source="{local:ImageResource AppName.Images.dot.png}"
           TintColor="{StaticResource PlaceholderColor}"
           VerticalOptions="Center"
           WidthRequest="17" />

                           

如果图像没有以xamarin格式呈现iOS图像,我想更改TintColor



您可以编写
Image
的自定义渲染器来更改色调颜色和
UIImageRenderingMode

在Xamarin.forms中:

public class myImage : Image
{

}
在iOS项目中:

[assembly: ExportRenderer(typeof(myImage), typeof(myImageRenderer))]
namespace WorkingWithImages.iOS
{
    public class myImageRenderer : ImageRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<Image> e)
        {
            base.OnElementChanged(e);

            Control.TintColor = UIColor.Red;

        }

        protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            base.OnElementPropertyChanged(sender, e);

            if (Control.Image != null)
            {
                UIImage image = Control.Image;

                image = image.ImageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate);

                Control.Image = image;
            }
        }
    }
}
[程序集:ExportRenderer(typeof(myImage)、typeof(myImageRenderer))]
名称空间使用images.iOS
{
公共类myImageRenderer:ImageRenderer
{
受保护的覆盖无效OnElementChanged(ElementChangedEventArgs e)
{
基础。一个要素发生变化(e);
Control.TintColor=UIColor.Red;
}
受保护的覆盖无效OnElementPropertyChanged(对象发送方,PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(发送方,e);
if(Control.Image!=null)
{
UIImage=Control.image;
image=image.ImageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate);
控件。图像=图像;
}
}
}
}

该软件包确实过时了,甚至其所有者都已将其存档。我不知道你是否可以给一个
png
图像着色,也许可以试试svg图像。谢谢!是否可以更改嵌入图像的色调颜色?是否有其他库?谢谢@Jack Hua,我使用的是相同的代码图像渲染模式已更改,但静止图像的色调颜色未更改。静态无效显示警告(myImage tintedImage,bool show){if(show){if(tintedImage.TintColor!=Color.Gray){tintedImage.Source=“dot.png”;tintedImage.TintColor=Color.Gray;}}否则{if(tintedImage.TintColor!=Color.Green){tintedImage.Source=“checkcircle.png”;tintedImage.TintColor=Color.Green;}}}}是否在此处添加断点以查看代码是否已执行?是的,已执行,图像源已更改,但TintColor未更改将在AlwaysTemplate下更改TintColor?你能试试其他的渲染模式吗?谢谢,它正在工作。对附加着色颜色的微小更改“if(Control.Image!=null){Control.Image=Control.Image.ImageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate);Control.TintColor=((myImage)元素).TintColor.ToUIColor();}”