色调颜色不适用于 iOS 中 Xamarin Forms 中的图像?

Tint color is not working for image in Xamarin Forms in iOS?

我正在使用 this nuget 包处理 xamarin 形式的嵌入式图像色调颜色,在 android 中工作正常,但在 iOS 中工作不正常。 根据我的搜索,我将渲染模式用于 AlwaysTemplate,但在我的场景中我无法使用渲染模式。 我正在做的是使用来自解决方案文件的图像引用,而不是来自 iOS assets.

下面是我的代码片段。

 <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;
            }
        }
    }
}