1

I'm completely new to xamarin.forms.

I used XLabs library to add checkboxes in my PCL project (Xamarin Forms).

When I run my app UWP ARM in Debug mode there's no error, but when I run the app in Release mode the checkboxes ARE never shown.

Is there any setting that I need to configure?

Greta
  • 111
  • 1
  • 1
  • 11
  • It seems that this library has some problems, please see [this](https://stackoverflow.com/questions/30772510/how-to-add-checkbox-in-xamarin-forms-in-xaml-file) in order to get other approach. – ganchito55 Jun 23 '17 at 09:19
  • I try these solutions but they don't help me. – Greta Jun 23 '17 at 10:43
  • 2
    XLabs are deprecated. Don't use it. For checkbox, why don't you use the Switch control? – hugo Jun 23 '17 at 13:50
  • Because I need to add a list of answers for a type of question and the Switch control isn't a solution for this of project. – Greta Jun 28 '17 at 08:30

1 Answers1

4

As @hugo said that the XLabs library is no longer maintained. It may not work with newer versions of Xamarin.Forms. For your requirement, you could use Switch control to replacing checkbox or use custom checkbox control. The following code implemented a simple checkbox. For more please refer to Introduction to Custom Renderers.

CustomCheckBox.cs

public class CustomCheckBox : View
{
    public static readonly BindableProperty CheckedProperty =
    BindableProperty.Create("Checked", typeof(bool), typeof(CustomCheckBox), default(bool));

    public bool Checked
    {
        get { return (bool)GetValue(CheckedProperty); }
        set { SetValue(CheckedProperty, value); }
    }

}

CustomCheckBoxRenderer.cs

[assembly: ExportRenderer(typeof(CustomCheckBox), typeof(CustomCheckBoxRenderer))]
namespace LabsTest.UWP
{
    public class CustomCheckBoxRenderer : ViewRenderer<CustomCheckBox, Windows.UI.Xaml.Controls.CheckBox>
    {
        protected override void OnElementChanged(ElementChangedEventArgs<CustomCheckBox> e)
        {
            base.OnElementChanged(e);
            if (Control == null)
            {
                SetNativeControl(new Windows.UI.Xaml.Controls.CheckBox());
            }
            if (Control != null)
            {
                Control.IsChecked = Element.Checked;
            }
        }
        protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            base.OnElementPropertyChanged(sender, e);
            if (e.PropertyName == nameof(Element.Checked))
            {
                UpdateStatus();
            }
        }
        private void UpdateStatus()
        {
            Control.IsChecked = Element.Checked;
        }
    }
}

Usage

<StackLayout HorizontalOptions="Center" VerticalOptions="Center">
    <local:CustomCheckBox x:Name="MyCheckBox" Checked="True">
    </local:CustomCheckBox>
</StackLayout>

enter image description here

Nico Zhu
  • 32,367
  • 2
  • 15
  • 36
  • Finally I added a custom renderer. Thank you! – Greta Jun 28 '17 at 08:32
  • I am using this implementation but when user checks the checkbox OnElementPropertyChanged never triggers for property Checked, but only for IsFocused . – user2297037 Nov 08 '17 at 12:05
  • 1
    I finally solved adding events subscription `Control.Checked += (s,r)=> { Element.Checked = true; };` and `Control.Unchecked += (s, r) => { Element.Checked = false; };` in the method `OnElementChanged`. – user2297037 Nov 08 '17 at 16:19