Xamarin.forms: [Bug] CheckBox causing crash in Android Fragment

Created on 11 Jul 2019  路  8Comments  路  Source: xamarin/Xamarin.Forms

Description

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.

Steps to Reproduce

  1. There's a repo here
  2. You can unload the iOS or UWP projects.
  3. 3.

Expected Behavior

CheckBox element renders without crashing

Actual Behavior

App crashes on call of InitialiseComponent() in the code behind of the XAML where the CheckBox element has been placed.

Basic Information

  • Version with issue: XamarinForms 4.1.0.xxxx
  • Last known good version: No CheckBox support for earlier versions afaik
  • IDE: VS2019 16.0.4, also VS2019 15.9.3
  • Platform Target Frameworks:

    • Android: <9.0>
  • Android Support Library Version:

  • Affected Devices: Samsung S8

embedding 馃摝 4 high Android bug

Most helpful comment

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));

All 8 comments

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"
image

Was this page helpful?
0 / 5 - 0 ratings