Search code examples
c#xamlmaui

MAUI app crashes while not debugging, InvalidCastException for TextColor


I made a MAUI app which I can run and debug in the emulator but whenever I run it without debugging (in either the emulator or on a physical device) it crashes while opening some pages. I enabled reporting all exceptions in Visual Studio and now while debugging I get lots of strange errors one of which being an InvalidCastException. It seems, the framework is somehow trying to interpret some TextColor values in XAML and fails on that.

On the page where this exception gets thrown I deleted all the TextColor value assignments and unfortunately I still get the exception.

If anyone knows a solution to this, I'll appreciate.

{System.InvalidCastException: Specified cast is not valid. at Microsoft.Maui.Controls.TextElement.OnTextColorPropertyChanged(BindableObject bindable, Object oldValue, Object newValue) in D:\a_work\1\s\src\Controls\src\Core\TextElement.cs:line 24 at Microsoft.Maui.Controls.BindableObject.SetValueActual(BindableProperty property, BindablePropertyContext context, Object value, Boolean currentlyApplying, SetValueFlags attributes, SetterSpecificity specificity, Boolean silent) in D:\a_work\1\s\src\Controls\src\Core\BindableObject.cs:line 643 at Microsoft.Maui.Controls.BindableObject.SetValueCore(BindableProperty property, Object value, SetValueFlags attributes, SetValuePrivateFlags privateAttributes, SetterSpecificity specificity) in D:\a_work\1\s\src\Controls\src\Core\BindableObject.cs:line 569 at Microsoft.Maui.Controls.BindableObject.SetValue(BindableProperty property, Object value, SetterSpecificity specificity) in D:\a_work\1\s\src\Controls\src\Core\BindableObject.cs:line 503 at Microsoft.Maui.Controls.Setter.Apply(BindableObject target, SetterSpecificity specificity) in D:\a_work\1\s\src\Controls\src\Core\Setter.cs:line 81 at Microsoft.Maui.Controls.Style.ApplyCore(BindableObject bindable, Style basedOn, SetterSpecificity specificity) in D:\a_work\1\s\src\Controls\src\Core\Style.cs:line 176 at Microsoft.Maui.Controls.Style.Microsoft.Maui.Controls.IStyle.Apply(BindableObject bindable, SetterSpecificity specificity) in D:\a_work\1\s\src\Controls\src\Core\Style.cs:line 107 at Microsoft.Maui.Controls.MergedStyle.SetStyle(IStyle implicitStyle, IList`1 classStyles, IStyle style) in D:\a_work\1\s\src\Controls\src\Core\MergedStyle.cs:line 211 at Microsoft.Maui.Controls.MergedStyle.set_Style(IStyle value) in D:\a_work\1\s\src\Controls\src\Core\MergedStyle.cs:line 46 at Microsoft.Maui.Controls.NavigableElement.<>c.<.cctor>b__22_0(BindableObject bindable, Object oldvalue, Object newvalue) in D:\a_work\1\s\src\Controls\src\Core\Shell\NavigableElement.cs:line 21 at Microsoft.Maui.Controls.BindableObject.SetValueActual(BindableProperty property, BindablePropertyContext context, Object value, Boolean currentlyApplying, SetValueFlags attributes, SetterSpecificity specificity, Boolean silent) in D:\a_work\1\s\src\Controls\src\Core\BindableObject.cs:line 643 at Microsoft.Maui.Controls.BindableObject.SetValueCore(BindableProperty property, Object value, SetValueFlags attributes, SetValuePrivateFlags privateAttributes, SetterSpecificity specificity) in D:\a_work\1\s\src\Controls\src\Core\BindableObject.cs:line 569 at Microsoft.Maui.Controls.BindableObject.SetValue(BindableProperty property, Object value) in D:\a_work\1\s\src\Controls\src\Core\BindableObject.cs:line 474 at Microsoft.Maui.Controls.Xaml.ApplyPropertiesVisitor.TrySetValue(Object element, BindableProperty property, Boolean attached, Object value, IXmlLineInfo lineInfo, IServiceProvider serviceProvider, Exception& exception) in D:\a_work\1\s\src\Controls\src\Xaml\ApplyPropertiesVisitor.cs:line 631}

enter image description here


Solution

  • So, the solution was to enable catching all exceptions in VS (Debug > Windows > Exception Settings Ctrl+Alt+E). Then I was able to catch the InvalidCastException. Then I found the simplest XAML page in my app where this exception was thrown (apparently, it's thrown by the XAML interpreter so I can't just debug it in classic step-by-step manner). Then I erased all the content in that page (remember to make a backup) and then I started to add in all the components one by one and restarting the app until I saw where the error started.

    Turns out, I had a Button which had a style applied to it (Style="{StaticResource PrimaryButton}") and the style's definition was in the App.xaml file.

    <Style x:Key="PrimaryButton" TargetType="Button">
        <Setter Property="BackgroundColor" Value="{StaticResource Primary}"/>
        <Setter Property="TextColor" Value="#e6eaed"/>
    </Style>
    

    The definition contained a Setter for the TextColor property and a Value for it. For some reason the XAML interpreter had a problem with it. After deleting the line with the Setter the error disappeared and the app started to work correctly. And the TextColor is inherited from some other object so it's still the same as I need it (lucky me).