Xamarin.forms 图像着色按钮Android Xamarin窗体

Xamarin.forms 图像着色按钮Android Xamarin窗体,xamarin.forms,xamarin.android,Xamarin.forms,Xamarin.android,我尝试为Android创建一个自定义渲染器,通过图像继承到ButtonRenderer。我希望通过编程方式更改图像的颜色,如iOS上的TintColor 例如,我的自定义渲染器iOS: public class IconButtonRenderer : ButtonRenderer { protected override void OnElementChanged(ElementChangedEventArgs<Button> e) { base.

我尝试为Android创建一个自定义渲染器,通过图像继承到ButtonRenderer。我希望通过编程方式更改图像的颜色,如iOS上的
TintColor

例如,我的自定义渲染器iOS:

public class IconButtonRenderer : ButtonRenderer
{

    protected override void OnElementChanged(ElementChangedEventArgs<Button> e)
    {
        base.OnElementChanged(e);
        var button = e.NewElement;
    }

    public override void Draw(CoreGraphics.CGRect rect)
    {
        base.Draw(rect);
        // Here I change color of Image in Red.
        Control.ImageView.Image = Control.ImageView.Image.ImageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate);
        Control.ImageView.TintColor = UIColor.Red;
    }


}
公共类IconButtonRenderer:ButtonRenderer
{
受保护的覆盖无效OnElementChanged(ElementChangedEventArgs e)
{
基础。一个要素发生变化(e);
var按钮=e.NewElement;
}
公共覆盖无效绘制(CoreGraphics.CGRect)
{
基础绘制(rect);
//在这里,我改变了红色图像的颜色。
Control.ImageView.Image=Control.ImageView.Image.ImageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate);
Control.ImageView.TintColor=UIColor.Red;
}
}
在Android上:
Control
是一个
Android.Widget.Button
并且没有图像属性,但是
Xamarin.Forms.Button
具有属性图像,并且对于Android使用
Android.Widget.Button

如何使用Android格式访问图像以更改颜色?

在按钮上可以使用“设置颜色过滤器(颜色)”

您可以使用ContextCompat获取颜色:

var color=new color(ContextCompat.GetColor(Context,Resource.color.Green));

然后应用它:

yourButton.SetColorFilter(color)

不确定是否需要使用ImageButton而不是简单的按钮

图像着色按钮Android Xamarin窗体

解决方案1: 正如@Mathias Kirkegaard所说,在您的
按钮说明中,您可以使用更改颜色:

Control.Background.SetColorFilter(Android.Graphics.Color.Blue, PorterDuff.Mode.SrcIn);
它在原生Android中确实可以工作,但在Xamarin.Forms中有一些问题,它只改变了
按钮的背景色,如下所示:。希望有人能找到解决办法,如果我找到解决这个问题的办法,我会回来更新我的答案

解决方案2: 您可以使用plugin:并添加一个click事件来实现相同的功能

  • 安装Plugin.CrossPlatformTintedImage
  • nuget软件包
  • 在iOS、Android和UWP项目中初始化渲染器,如下所示:

    Xamarin.Forms.Init();    
    TintedImageRenderer.Init();
    
  • 在Xaml中:

    <ContentPage 
        ...
        xmlns:controls="clr-namespace:Plugin.CrossPlatformTintedImage.Abstractions;assembly=Plugin.CrossPlatformTintedImage.Abstractions"
         ...>
         ...
         <controls:TintedImage x:Name="buttonImage" TintColor="Blue" Source="redDis.png"/>
    ...
    </ContentPage>
    
  • 解决方案3: 如果需要使用
    ImageButton
    ,可以参考此解决方案。我没有测试它,但你可以使用

    • XLab源代码

    • XLab源代码


    SetColorFilter()找不到button
    Android.Widget.button
    可能我需要ImageButton,但
    Xamarin.Forms
    没有ImageButton。解决方案1不是我的目标。解决方案3:“此项目不再维护。它可能无法与较新版本的Xamarin.Forms一起使用。”解决方案2是最好的,但我尝试使用
    TapgestureRecognitizer
    创建渲染器,因为我需要在解决方案中使用多次。@MatthieuLemonnier,您可以在
    Xaml
    中设置
    TapGestureRecognitizer
    ,就像这样。Thx,解决方案2就是答案。@MatthieuLemonnier,happy coding.)
    var tapGestureRecognizer = new TapGestureRecognizer();
    tapGestureRecognizer.Tapped += (s, e) => 
    {
        // handle the tap
    };
    buttonImage.GestureRecognizers.Add(tapGestureRecognizer);