question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:16 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
bijingtoncommented, Apr 24, 2022

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

0reactions
VladislavAntonyukcommented, Sep 15, 2022

I believe it can be closed with 1.3.0 release. You can use SetShouldSuppressExceptionsInConverters

Read more comments on GitHub >

github_iconTop Results From Across the Web

EnumToIntConverter - .NET MAUI Community Toolkit
The EnumToIntConverter is a converter that allows you to convert a standard Enum (extending int) to its underlying primitive int type.
Read more >
What is the correct exception to throw for unhandled enum ...
I create my own BoneheadedException class which I throw whenever I detect a programming error for which no FCL exception type is more ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found