1
votes

I am trying to render a checkbox in a Xamarin Forms app. There is nothing rendered at runtime, as far as I can tell the renderer is not even getting called.

Does anyone understand what I am missing or doing incorrectly?

Here is my class in Forms:

public class LegalCheckbox : View
{
    public LegalCheckbox ()
    {

    }
}

And my custom renderer class in Droid:

public class CheckBoxRenderer : ViewRenderer<LegalCheckbox, CheckBox>
{
    protected override void OnElementChanged (ElementChangedEventArgs<LegalCheckbox> e)
    {
        base.OnElementChanged (e);
        CheckBox control = new Android.Widget.CheckBox(this.Context);
        control.Checked = false;
        control.Text = "I agree to terms";
        control.SetTextColor (Android.Graphics.Color.Rgb (60, 60, 60));
        this.SetNativeControl(control);
    }
}

Along with the Assembly Directive:

[assembly: ExportRenderer(typeof(demo.LegalCheckbox), typeof(demo.Droid.CheckBoxRenderer))]
2
does taking out the empty constructor make a difference?Jason

2 Answers

2
votes

Took your code and fired up a new project with it. The code appears to function fine.

Only thin I can think that might be causing you an issue is the location of you assembly attribute. I typically place them just above the namespace declaration in the same file as my renderer.

I threw what I created up on my github maybe you can spot the difference.

https://github.com/DavidStrickland0/Xamarin-Forms-Samples/tree/master/RendererDemo

@Thibault D. Xlabs isn't a bad project but its basically just all the code the opensource community came up with during the first year or so of Xamarin.Forms life. Its not really "Their Labs projects" and considering how much of it is marked up with Alpha Beta and the number of bugs in their issues page it's probably best not to imply that the Xamarin company has anything to do with it.

0
votes

I am not sure if that is the issue but it would make more sense to me if your LegalCheckbox would inherit from a InputView rather than View.

Also, even if Xamarin.Forms does not have a Checkbox control you can still have a look at their "Labs" project here:

https://github.com/XLabs/Xamarin-Forms-Labs/wiki/Checkbox-Control

(And I can actually see that they inherit from View...)