Xamarin crashes with some types of nullable attached properties.

midixmidix LVMember ✭✭

Please take a look at the following simplified code for two attached properties:

        public static class TestAttachedProps
        {
            public static readonly BindableProperty HasSomethingProperty =
                BindableProperty.CreateAttached("HasSomething",
                    typeof(bool?),
                    typeof(TestAttachedProps),
                    null, 
                    propertyChanged: OnHasSomethingChanged);

            public static readonly BindableProperty ColorOfSomethingProperty =
                BindableProperty.CreateAttached("ColorOfSomething",
                    typeof(Color?),
                    typeof(TestAttachedProps),
                    null,
                    propertyChanged: OnColorOfSomethingChanged);

            private static void OnHasSomethingChanged(BindableObject bindable, object oldValue, object newValue)
            {
                var view = bindable as View;
                if (view == null)
                    return;
            }

            private static void OnColorOfSomethingChanged(BindableObject bindable, object oldValue, object newValue)
            {
                var view = bindable as View;
                if (view == null)
                    return;
            }

            public static void SetHasSomething(BindableObject view, bool? hasSomething)
            {
                view.SetValue(HasSomethingProperty, hasSomething);
            }

            public static bool? GetHasSomething(BindableObject view)
            {
                return (bool?)view.GetValue(HasSomethingProperty);
            }

            public static void SetColorOfSomething(BindableObject view, Color? colorOfSomething)
            {
                view.SetValue(ColorOfSomethingProperty, colorOfSomething);
            }

            public static Color? GetColorOfSomething(BindableObject view)
            {
                return (Color?)view.GetValue(ColorOfSomethingProperty);
            }
        }

XAML:

  <Button Text="Test 1" test:TestAttachedProps.HasSomething="False" test:TestAttachedProps.ColorOfSomething="Transparent">
  </Button>

HasSomething property works as expected, I receive OnHasSomethingChanged. But after that, Xamarin crashes in InitializeComponent() with System.AccessViolationException. As I understand, Xamarin knows how to work with bool? but does not handle Color? well. As soon as I replace all Color? with Color it starts working again.

I hope I might fix this with custom valueconverter, but I'm not sure what should I return from my converter when the value is null because in that case I really need it to remain null (to specify unset color and to trigger OnColorOfSomethingChanged when applying ColorOfSomething to an element).

And how to tell Xamarin that this converter should be used for all cases when my ColorOfSomethingProperty is accessed. Is there some way to specify default converter for attached property? I would like to avoid specifying converter manually in XAML for each ColorOfSomething.

Sign In or Register to comment.