Maui: [Bug] EnumToIntConverter throws an unhandled exception when the picker tries to convert -1

Description

The EnumToIntConverter doesn’t mix well sometimes with Pickers in MAUI. Sometimes the Picker seems to try to convert -1, and it of course fails since the enum doesn’t have that value.

I have created a gif showing the issue in the app I am developing. If I find some time I’ll try to create a repo project, but in short the issue seems related to MAUI reloading a hidden Picker. And the index for some reason is set to -1 at one point.

I have solved this for the mean time in my code by copying the source code from here: https://github.com/CommunityToolkit/Maui/blob/6d3ecd66810077bb102436cf2d264219d3cd7c04/src/CommunityToolkit.Maui/Converters/EnumToIntConverter.shared.cs#L41

And replacing that line with a return null.

Link to Reproduction Sample

You can take a look at the code source of my project here: https://github.com/StockDrops/OpenStockApp/blob/a7cb138c8f8ef1acb6ba8cb196403b74e550ba0e/OpenStockApp/Views/ModelOptionsView.xaml#L120

I basically have a view with a picker and an enum, and when I load the items into the picker, navigate to another page with the another instance of the view inside, load items again, and then go back to the original view, the MAUI framework seems to set SelectedIndex to -1 and this of course breaks the converter since it throws that it doesn’t find -1 in the enum, but I have 0 control over this since it’s just the way MAUI seems to work.

Expected Behavior

Shoudn’t throw that exception for -1 seems it seems like an important value for MAUI.

Actual Behavior

Throws an exception, causing an unhandled app exception crashing the app.

Basic Information

  • Version with issue: RC1
  • Last known good version: Preview 14
  • IDE: VS 2022 Preview (latest)
  • Platform Target Frameworks:
    • UWP: This happens on Windows, I haven’t tested in on other frameworks.

Workaround

Copy the source code, change the line 120 to return null.

Reproduction imagery

Animation

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 16 (6 by maintainers)

Most upvoted comments

I would extend this question… If the default value is -1, I believe that the control isn’t fully initialized yet, so the MAUI framework shouldn’t trigger the Converters, Triggers, etc. I’m curious in which scenarios this happens.

@emorell96

Could you attach a small repro?

There is a link to a repro in the original post