C# 改变按钮图像的颜色

C# 改变按钮图像的颜色,c#,xamarin.forms,xamarin.android,renderer,C#,Xamarin.forms,Xamarin.android,Renderer,我有一个适用于Android的自定义按钮阅读器,我想更改用于按钮的图像的颜色 这是我的按钮说明者: public class VolosBaseButtonRenderer : Xamarin.Forms.Platform.Android.AppCompat.ButtonRenderer { public VolosBaseButtonRenderer(Context context) : base(context) { } protected override

我有一个适用于Android的自定义
按钮阅读器
,我想更改用于按钮的图像的颜色

这是我的
按钮说明者

public class VolosBaseButtonRenderer : Xamarin.Forms.Platform.Android.AppCompat.ButtonRenderer {
        public VolosBaseButtonRenderer(Context context) : base(context) { }

        protected override void OnElementChanged(ElementChangedEventArgs<Button> e) {
            base.OnElementChanged(e);

            // What I must do here for change the Image color????

        }

        protected override AppCompatButton CreateNativeControl() {
            var context = new ContextThemeWrapper(Context, Resource.Style.Widget_AppCompat_Button_Borderless);
            var button = new AppCompatButton(context, null, Resource.Style.Widget_AppCompat_Button_Borderless);
            return button;
        }    
    }
如果可能的话,我可以在按钮上应用样式(但我不知道怎么做)

任何方法都可以


谢谢大家!

假设您的自定义按钮有两个属性,
CustomImage
,它存储资源图像名称的字符串,即“icon.png”,和另一个
ImageTintColor
,它存储
Xamarin.Forms.Color
对象,您可以这样使用它:

var button = (CustomButton)Control.Element;
using (var image = GetScaleDrawable(Resources.GetDrawable(button.CustomImage.Split('.')[0]),
    (int)button.WidthRequest, 
    (int)button.HeightRequest))
{
    if (button.ImageTintColor != Xamarin.Forms.Color.Default)
        image.SetColorFilter(button.ImageTintColor.ToAndroid(), PorterDuff.Mode.SrcAtop);

    this.Control.SetPadding(0, 0, 0, 0);
    this.Control.SetCompoundDrawables(null, image, null, null);
}

它不应该只是button.setcolorfilter(Color.Blue,PortterDuff.Mode.SrcIn);不确定为什么要使用“Control.Background”,它也不是您在图像按钮上着色的背景。
var button = (CustomButton)Control.Element;
using (var image = GetScaleDrawable(Resources.GetDrawable(button.CustomImage.Split('.')[0]),
    (int)button.WidthRequest, 
    (int)button.HeightRequest))
{
    if (button.ImageTintColor != Xamarin.Forms.Color.Default)
        image.SetColorFilter(button.ImageTintColor.ToAndroid(), PorterDuff.Mode.SrcAtop);

    this.Control.SetPadding(0, 0, 0, 0);
    this.Control.SetCompoundDrawables(null, image, null, null);
}