Search code examples
xamarinxamarin.androidxamarin.formsdatatrigger

Xamarin Forms DataTrigger causes NullReferenceException


I am attempting to use a simple DataTrigger element within the XAML of a Xamarin Forms page:

<Frame BackgroundColor="Red" HorizontalOptions="Fill" VerticalOptions="FillAndExpand">
    <Frame.Triggers>
        <DataTrigger TargetType="Frame" Binding="{Binding IsValid}" Value="True">
            <Setter Property="BackgroundColor" Value="{x:Static Color.Lime}" />
        </DataTrigger>
    </Frame.Triggers>
</Frame>

This configuration crashes the application with what is effectively a NullReferenceException:

Java.Lang.NullPointerExceptionAttempt to invoke virtual method 'boolean android.graphics.Bitmap.isMutable()' on a null object reference

If I comment out the Setter in the above sample, the application runs normally but, of course, the trigger doesn't work.

Can anyone please suggest what I am doing wrong?


Solution

  • Found it!

    I had a look at the decompiled version. This is a bug that occurs when the frame has a height of 0.

    enter image description here

    And this causes the error in FrameOnPropertyChanged

    enter image description here

    Adding Padding="1" or HeightRequest="1" WidthRequest="1" to your Frame should fix it, except there is something forcing it to 0.