I'm embedding XamarinForms in native apps as Fragments, based largely on the pattern described here.
After upgrading to XamarinForms 4.1.0.555618, I have decided to give the CheckBox element a go, and whilst it works fine on iOS, on Android it crashes with the debug output giving the error: CheckBox only works with Theme.AppCompat theme (or descendant)
After changing some themes and inheritance to integrate AppCompat, I get the same crash, but without the above warning.
CheckBox element renders without crashing
App crashes on call of InitialiseComponent() in the code behind of the XAML where the CheckBox element has been placed.
Platform Target Frameworks:
Android Support Library Version:
Affected Devices: Samsung S8
This is happening because embedded pages never get LoadApplication called, and LoadApplication is where the old renderers get swapped out for the AppCompat renderers. And CheckBox doesn't have an old-style renderer, just a shim renderer for the Previewer - that's what throws the CheckBox only works with Theme.AppCompat theme (or descendant) error.
We'll need to either add an option for users who want the AppCompat renderers to specify that when creating the embedded fragment, or a way to specify that during Forms.Init, or (my preference) we'll need to detect that we're embedded in an AppCompatActivity and adjust accordingly.
@johnallan78 In the meantime, you can work around this problem by manually registering for the AppCompat renderer for the CheckBox - before calling Forms.Init, add the following line:
Registrar.Registered.Register(typeof(Xamarin.Forms.CheckBox), typeof(Xamarin.Forms.Platform.Android.CheckBoxRenderer));
I have this issue as well. I tried the suggested work around and while it prevents the crash from happening, it doesn't actually render anything.
@hartez @sung-insightly I've found that I can render a CheckBox, but only if the activity which starts the Fragment Transaction extends AppCompatActivity. I don't know if that warrants closing this bug, as I have activities that don't extend this class where the CheckBox obviously can't be used.
Same issue. Couldn't get this to work with the workarounds. Using SyncFusion checkbox for now. Hope this will be solved soon.
I am also experiencing this issue.
This is still not fixed?
@sung-insightly you need to have a default theme so that will Checkbox is painted on the window.
@hartez your workaround Registrar.Registered.Register(typeof(Xamarin.Forms.CheckBox), typeof(Xamarin.Forms.Platform.Android.CheckBoxRenderer)); is valid
addd this line after the xmlns declarations on the xaml page it will work worked for me tested
Visual="Material"

Most helpful comment
This is happening because embedded pages never get
LoadApplicationcalled, andLoadApplicationis where the old renderers get swapped out for the AppCompat renderers. And CheckBox doesn't have an old-style renderer, just a shim renderer for the Previewer - that's what throws theCheckBox only works with Theme.AppCompat theme (or descendant)error.We'll need to either add an option for users who want the AppCompat renderers to specify that when creating the embedded fragment, or a way to specify that during Forms.Init, or (my preference) we'll need to detect that we're embedded in an AppCompatActivity and adjust accordingly.
@johnallan78 In the meantime, you can work around this problem by manually registering for the AppCompat renderer for the CheckBox - before calling Forms.Init, add the following line:
Registrar.Registered.Register(typeof(Xamarin.Forms.CheckBox), typeof(Xamarin.Forms.Platform.Android.CheckBoxRenderer));