Why is my custom ButtonRenderer not working?

前端 未结 2 1775
无人及你
无人及你 2020-12-10 21:42

I\'m trying to create a custom ButtonRenderer for Xamarin.Forms. Here is a simple test I\'ve been trying to put up together following some tutorials, but I can\'s seem to ma

相关标签:
2条回答
  • 2020-12-10 22:13

    replace CustomButtonRenderer code to below code.

    [assembly: ExportRenderer(typeof(Xamarin.Forms.Button), typeof(CustomButtonRenderer))]
    namespace TestProject.Droid.CustomRenderers
    {
    public class CustomButtonRenderer: ButtonRenderer
    {
        public CustomButtonRenderer(Context context) : base(context)
        {
        }
    
        protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.Button> e)
        {
            base.OnElementChanged(e);
            if (e.OldElement != null)
            {
                return;
            }
            var nativeButton = (Android.Widget.Button)this.Control;
            nativeButton.SetBackgroundColor(Android.Graphics.Color.Gray);
    
        }
    }
    }
    
    0 讨论(0)
  • 2020-12-10 22:20

    I'm still investigating on this. Your class gets shrunk because it's not statically linked in your PCL. You avoid that by giving a name to your classes like this:

    [Activity(Name = "somepackage.custombuttonrenderer")]
    public class CustomButtonRenderer: ButtonRenderer
    { }
    

    If you're able to target the minimum Android version to Android 5.0 (Api 21) this problem should disappear as another version of the Dex file is used.

    0 讨论(0)
提交回复
热议问题