Share via

BINDING CONVERTER THROWS EXCEPTION

Giorgio Sfiligoi 616 Reputation points
2024-10-16T18:08:53.4533333+00:00

HELP

I have a simple binding like that:

    <Entry Text="{Binding Amount"     OK

    <Entry Text="{Binding Amount, Converter={StaticResource amountConverter}}"  THROWS an exception.

Here is the complete page:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:cvt="clr-namespace:TestVari.Converters"
             x:Class="TestVari.Pages.TestMyKeyboardPage"
             Title="Test">
    <ContentPage.Resources>
        <cvt:AmountConverter x:Key="amountConverter" />
    </ContentPage.Resources>
    <VerticalStackLayout>
        <Label Text="dummy label" />
        <Entry Text="{Binding Amount, Converter={StaticResource amountConverter}}"
               WidthRequest="300" IsReadOnly="True"
               VerticalOptions="Start" HorizontalOptions="Start"
               TextColor="Blue" BackgroundColor="WhiteSmoke">
            <Entry.GestureRecognizers>
                <TapGestureRecognizer Tapped="OnFieldTapped" />
            </Entry.GestureRecognizers>
        </Entry>
        <Button Text="37" WidthRequest="60" HorizontalOptions="Start" Clicked="On37" />
    </VerticalStackLayout>
</ContentPage>

When I try to navigate to this page I have the following exception:

System.Reflection.TargetInvocationException

Message=Exception has been thrown by the target of an invocation.

Stack:

 	0xFFFFFFFFFFFFFFFF in Android.Runtime.RuntimeNativeMethods.monodroid_debugger_unhandled_exception	C#
 	0x1A in Android.Runtime.JNINativeWrapper._unhandled_exception at /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Android.Runtime/JNINativeWrapper.g.cs:12,5	C#
 	0x1D in Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PP_V at /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Android.Runtime/JNINativeWrapper.g.cs:23,26	C#
 	0x17 in System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw	C#
 	0x6 in System.Threading.Tasks.Task.<>c.<ThrowAsync>b__128_0	C#
 	0xC in Android.App.SyncContext. at /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Android.App/SyncContext.cs:36,19	C#
 	0xE in Java.Lang.Thread.RunnableImplementor.Run at /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Java.Lang/Thread.cs:37,6	C#
 	0x8 in Java.Lang.IRunnableInvoker.n_Run at /Users/runner/work/1/s/xamarin-android/src/Mono.Android/obj/Release/net8.0/android-34/mcw/Java.Lang.IRunnable.cs:84,4	C#
 	0x8 in Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PP_V at /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Android.Runtime/JNINativeWrapper.g.cs:22,5	C#

What makes me very confused is that I have other pages with similar binding and the same 'amountConverter', and they work fine.

Can anybody give me a clue on how to address this issue?

Developer technologies | .NET | .NET MAUI
{count} votes

1 answer

Sort by: Most helpful
  1. Giorgio Sfiligoi 616 Reputation points
    2024-10-19T07:07:05.2+00:00

    I changed the ConvertBack of AmountConverter to: return 0;

    Now it does not throw the exception when I changed Label to Entry in the ConvertersPage; however, to obtain the same experience as before, it is necessary to use Mode=OneWay:

            <Entry Grid.Row="1" Grid.Column="2" BackgroundColor="Aquamarine" HorizontalOptions="Fill"
                   Text="{Binding Amount, Converter={StaticResource AmountConverter}, Mode=OneWay}"
                   TextColor="{Binding Amount, Converter={StaticResource AmountColorConverter}}"/>
    
    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.