[Bug] EnumToIntConverter throws an unhandled exception when the picker tries to convert -1
See original GitHub issueDescription
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
Issue Analytics
- State:
- Created a year ago
- Comments:16 (6 by maintainers)
Top GitHub Comments
There is a link to a repro in the original post
I believe it can be closed with 1.3.0 release. You can use SetShouldSuppressExceptionsInConverters