Maui: [BUG] Non-nullable are not valid InvalidOperationException is thrown when using VariableMultiValueConverter

Is there an existing issue for this?

  • I have searched the existing issues

Did you read the “Reporting a bug” section on Contributing file?

Current Behavior

System.InvalidOperationException: ‘Non-nullable are not valid’

Expected Behavior

MultiBinding works as it used to in prior versions.

Steps To Reproduce

1- Give MainPage a reference name x:Name="ThisPage"

2- Copy and paste the following code in MainPage:

    <ContentPage.Resources>
        <toolkit:VariableMultiValueConverter ConditionType="All" x:Key="VariableMultiValueConverterAll" />
        <toolkit:InvertedBoolConverter x:Key="InvertedBoolConverter" />
    </ContentPage.Resources>

    <ScrollView>
        <VerticalStackLayout
            Padding="30,0"
            Spacing="25">
            <VerticalStackLayout.IsEnabled>
                <MultiBinding Converter="{StaticResource VariableMultiValueConverterAll}">
                    <Binding Path="IsEnabled" Source="{x:Reference ThisPage}" Converter="{StaticResource InvertedBoolConverter}" />
                    <Binding Path="IsVisible" Source="{x:Reference ThisPage}" Converter="{StaticResource InvertedBoolConverter}" />
                </MultiBinding>
            </VerticalStackLayout.IsEnabled>
        </VerticalStackLayout>
    </ScrollView>

3- Make sure CommunityToolkit.Maui 9.0.2 is installed 4- Non-nullable are not valid exception is thrown 5- Revert back to CommunityToolkit.Maui 9.0.1 to avoid this issue

Non-nullable2

Link to public reproduction project repository

https://github.com/usefulBeeing/MauiNonNullableError

Environment

- .NET MAUI CommunityToolkit: 9.0.2
- OS: Windows 10.0.22631 Build 22631
- .NET MAUI: 8.0.61

Anything else?

No response

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 12
  • Comments: 17 (7 by maintainers)

Most upvoted comments

The IsNotNullConverter throw same error on 9.0.2 in 9.0.1 is Ok

To add some context, the converters in the scenario reported are not through compiled bindings and therefore the targetType being passed into the InvertedBoolConverter is of type object. The fix might be to undo the original PR or handle the specific case for object.

I’ve started looking at it but life has managed to get in the way. It will get fixed at some point soon.

Please make sure you use 9.0.1 for now

There is a breaking change in InvertedBoolConverter

That converter throws an exception when the binded value is null. Before this release, it would not.

I understand why it is throwing now, because it does not know what to return when the bool is null.

But in the context of binding, while the BindingContext is not yet set, most boolean will be null. That does happen a lot.

I worked around that by replacing it with my own simple converter:

public class InvertedNullableBoolConverter : IValueConverter
{
    public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
    {
        if (value is bool b)
            return !b;

        return null;
    }

    public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture) 
        => throw new NotSupportedException();
}

No need to do complicated code involving reflection at runtime. Keep it simple.

@usefulBeeing Sorry, I hadn’t made it till step 5 while reading, it would’ve spared me some debugging time 😛 Anyway I’m glad that there is a solution (workaround) for the issue and thankful or the team, and especially @bijington, for taking the issue seriously and in such a timely manner. I hope it gets fixed soon 👍

Thanks for the details. I suspect the cause of this was this change https://github.com/CommunityToolkit/Maui/pull/1926 I thought I had tested all scenarios but I must have missed one. I can try to take a look 👍