maui.net-maui-android

Maui App runs in debug mode when attached to Visual Studio, but crashes standalone


I've narrowed the issue down to something in my Flyout menu xaml code, but cannot pinpoint the cause. My xaml:

        <VerticalStackLayout
            x:Name="Flyout"
            WidthRequest="260"
            HorizontalOptions="Start"
            Spacing="2"
            Grid.Row="1"
            BackgroundColor="Gold"
            TranslationX="-250">
            <HorizontalStackLayout HorizontalOptions="Start" HeightRequest="50" >
                <Image Source="addEmployee.jpg" Style="{StaticResource IconLabelStyle}" />
                <Label x:Name="AddEmployee" Text="Add Employee" Style="{StaticResource LabelStyle}" />
                <HorizontalStackLayout.GestureRecognizers>
                    <TapGestureRecognizer Tapped="OnMenuItemTap" CommandParameter="Add Another Participant"/>
                </HorizontalStackLayout.GestureRecognizers>
            </HorizontalStackLayout>
            <HorizontalStackLayout HorizontalOptions="Start" HeightRequest="50">
                <Image Source="assign.jpg" Style="{StaticResource IconLabelStyle}" />
                <Label x:Name="Assign" Text="Assign Employee To Section" Style="{StaticResource LabelStyle}" />
                <HorizontalStackLayout.GestureRecognizers>
                    <TapGestureRecognizer Tapped="OnMenuItemTap" CommandParameter="Re-Register"/>
                </HorizontalStackLayout.GestureRecognizers>
            </HorizontalStackLayout>
            <HorizontalStackLayout HorizontalOptions="Start" HeightRequest="50">
                <Image Source="text.jpg" Style="{StaticResource IconLabelStyle}" />
                <Label x:Name="sendtext" Text="Send Text" Style="{StaticResource LabelStyle}" />
                <HorizontalStackLayout.GestureRecognizers>
                    <TapGestureRecognizer Tapped="OnMenuItemTap" CommandParameter="ChatRoom"/>
                </HorizontalStackLayout.GestureRecognizers>
            </HorizontalStackLayout>
            <HorizontalStackLayout HorizontalOptions="Start" HeightRequest="50">
                <Image Source="myaccount.jpg" Style="{StaticResource IconLabelStyle}" />
                <Label x:Name="MyAccount" Text="My Account" Style="{StaticResource LabelStyle}" />
                <HorizontalStackLayout.GestureRecognizers>
                    <TapGestureRecognizer Tapped="OnMenuItemTap" CommandParameter="My Account"/>
                </HorizontalStackLayout.GestureRecognizers>
            </HorizontalStackLayout>
            <HorizontalStackLayout HorizontalOptions="Start" HeightRequest="50">
                <Image Source="myaccount.jpg" Style="{StaticResource IconLabelStyle}" />
                <Label x:Name="LogOut" Text="Log Off" Style="{StaticResource LabelStyle}" />
                <HorizontalStackLayout.GestureRecognizers>
                    <TapGestureRecognizer Tapped="OnMenuItemTap" CommandParameter="Log Off"/>
                </HorizontalStackLayout.GestureRecognizers>
            </HorizontalStackLayout>
        </VerticalStackLayout>

When I comment out the Flyout code, the app will run outside the debugger. I dumped the test device crash log, I found this in it:

03-05 20:52:00.570 10573  4825  4825 I MonoDroid: UNHANDLED EXCEPTION:
03-05 20:52:00.575 10573  4825  4825 I MonoDroid: Android.Runtime.JavaProxyThrowable: Exception of type 'Android.Runtime.JavaProxyThrowable' was thrown.
03-05 20:52:00.575 10573  4825  4825 I MonoDroid: 
03-05 20:52:00.575 10573  4825  4825 I MonoDroid:   --- End of managed Android.Runtime.JavaProxyThrowable stack trace ---
03-05 20:52:00.575 10573  4825  4825 I MonoDroid: android.runtime.JavaProxyThrowable: [System.InvalidCastException]: Specified cast is not valid.
03-05 20:52:00.575 10573  4825  4825 I MonoDroid:   at Microsoft.Maui.Controls.Xaml.ApplyPropertiesVisitor.SetPropertyValue + 0xb9(Unknown Source)
03-05 20:52:00.575 10573  4825  4825 I MonoDroid:   at Microsoft.Maui.Controls.Xaml.ApplyPropertiesVisitor.Visit + 0xf6(Unknown Source)
03-05 20:52:00.575 10573  4825  4825 I MonoDroid:   at Microsoft.Maui.Controls.Xaml.ElementNode.Accept + 0x90(Unknown Source)
03-05 20:52:00.575 10573  4825  4825 I MonoDroid:   at Microsoft.Maui.Controls.Xaml.ElementNode.Accept + 0x3e(Unknown Source)
03-05 20:52:00.575 10573  4825  4825 I MonoDroid:   at Microsoft.Maui.Controls.Xaml.ElementNode.Accept + 0x67(Unknown Source)
03-05 20:52:00.575 10573  4825  4825 I MonoDroid:   at Microsoft.Maui.Controls.Xaml.ElementNode.Accept + 0x67(Unknown Source)
03-05 20:52:00.576 10573  4825  4825 I MonoDroid:   at Microsoft.Maui.Controls.Xaml.ElementNode.Accept + 0x67(Unknown Source)
03-05 20:52:00.576 10573  4825  4825 I MonoDroid:   at Microsoft.Maui.Controls.Xaml.ElementNode.Accept + 0x67(Unknown Source)
03-05 20:52:00.576 10573  4825  4825 I MonoDroid:   at Microsoft.Maui.Controls.Xaml.RootNode.Accept + 0x81(Unknown Source)
03-05 20:52:00.576 10573  4825  4825 I MonoDroid:   at Microsoft.Maui.Controls.Xaml.XamlLoader.Visit + 0x97(Unknown Source)
03-05 20:52:00.576 10573  4825  4825 I MonoDroid:   at Microsoft.Maui.Controls.Xaml.XamlLoader.Load + 0x9d(Unknown Source)
03-05 20:52:00.576 10573  4825  4825 I MonoDroid:   at Microsoft.Maui.Controls.Xaml.XamlLoader.Load + 0x0(Unknown Source)
03-05 20:52:00.576 10573  4825  4825 I MonoDroid:   at Microsoft.Maui.Controls.Xaml.XamlLoader.Load + 0x29(Unknown Source)
03-05 20:52:00.576 10573  4825  4825 I MonoDroid:   at Microsoft.Maui.Controls.Xaml.Extensions.LoadFromXaml + 0x0(Unknown Source)
03-05 20:52:00.576 10573  4825  4825 I MonoDroid:   at MyApp.RegUser.InitializeComponent + 0x1(Unknown Source)
03-05 20:52:00.576 10573  4825  4825 I MonoDroid:   at MyApp.RegUser..ctor + 0xa0(Unknown Source)
03-05 20:52:00.576 10573  4825  4825 I MonoDroid:   at MyApp.Logon+<SignOn>d__6.MoveNext + 0x7b6(Unknown Source)
03-05 20:52:00.576 10573  4825  4825 I MonoDroid:   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw + 0x11(Unknown Source)
03-05 20:52:00.576 10573  4825  4825 I MonoDroid:   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess + 0x37(Unknown Source)
03-05 20:52:00.576 10573  4825  4825 I MonoDroid:   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification + 0x2d(Unknown Source)
03-05 20:52:00.576 10573  4825  4825 I MonoDroid:   at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd + 0x8(Unknown Source)
03-05 20:52:00.576 10573  4825  4825 I MonoDroid:   at System.Runtime.CompilerServices.TaskAwaiter.GetResult + 0x0(Unknown Source)
03-05 20:52:00.576 10573  4825  4825 I MonoDroid:   at MyApp.Logon+<<OnCounterClicked>b__5_0>d.MoveNext + 0x73(Unknown Source)
03-05 20:52:00.576 10573  4825  4825 I MonoDroid:   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw + 0x11(Unknown Source)
03-05 20:52:00.576 10573  4825  4825 I MonoDroid:   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess + 0x37(Unknown Source)
03-05 20:52:00.576 10573  4825  4825 I MonoDroid:   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification + 0x2d(Unknown Source)
03-05 20:52:00.576 10573  4825  4825 I MonoDroid:   at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd + 0x8(Unknown Source)
03-05 20:52:00.576 10573  4825  4825 I MonoDroid:   at System.Runtime.CompilerServices.TaskAwaiter.GetResult + 0x0(Unknown Source)
03-05 20:52:00.576 10573  4825  4825 I MonoDroid:   at MyApp.Logon+<OnCounterClicked>d__5.MoveNext + 0xf4(Unknown Source)
03-05 20:52:00.576 10573  4825  4825 I MonoDroid:   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw + 0x11(Unknown Source)
03-05 20:52:00.576 10573  4825  4825 I MonoDroid:   at System.Threading.Tasks.Task+<>c.<ThrowAsync>b__128_0 + 0x0(Unknown Source)
03-05 20:52:00.576 10573  4825  4825 I MonoDroid:   at Android.App.SyncContext+<>c__DisplayClass2_0.<Post>b__0 + 0x0(Unknown Source)
03-05 20:52:00.576 10573  4825  4825 I MonoDroid:   at Java.Lang.Thread+RunnableImplementor.Run + 0x8(Unknown Source)
03-05 20:52:00.576 10573  4825  4825 I MonoDroid:   at Java.Lang.IRunnableInvoker.n_Run + 0x0(Unknown Source)
03-05 20:52:00.576 10573  4825  4825 I MonoDroid:   at Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PP_V + 0x5(Unknown Source)
03-05 20:52:00.576 10573  4825  4825 I MonoDroid:   at mono.java.lang.RunnableImplementor.n_run(Native Method)
03-05 20:52:00.576 10573  4825  4825 I MonoDroid:   at mono.java.lang.RunnableImplementor.run(RunnableImplementor.java:29)
03-05 20:52:00.576 10573  4825  4825 I MonoDroid:   at android.os.Handler.handleCallback(Handler.java:958)
03-05 20:52:00.576 10573  4825  4825 I MonoDroid:   at android.os.Handler.dispatchMessage(Handler.java:99)
03-05 20:52:00.576 10573  4825  4825 I MonoDroid:   at android.os.Looper.loopOnce(Looper.java:230)
03-05 20:52:00.576 10573  4825  4825 I MonoDroid:   at android.os.Looper.loop(Looper.java:319)
03-05 20:52:00.576 10573  4825  4825 I MonoDroid:   at android.app.ActivityThread.main(ActivityThread.java:9063)
03-05 20:52:00.576 10573  4825  4825 I MonoDroid:   at java.lang.reflect.Method.invoke(Native Method)
03-05 20:52:00.576 10573  4825  4825 I MonoDroid:   at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:588)
03-05 20:52:00.576 10573  4825  4825 I MonoDroid:   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1103)
03-05 20:52:00.576 10573  4825  4825 I MonoDroid: 
03-05 20:52:00.576 10573  4825  4825 I MonoDroid:   --- End of managed Android.Runtime.JavaProxyThrowable stack trace ---

An error is caught on this line Specified cast is not valid. at Microsoft.Maui.Controls.Xaml.ApplyPropertiesVisitor.SetPropertyValue + 0xb9(Unknown Source)

I don't see any information in there that pinpoints the cast exception. Perhaps another set of eyes will.


Solution

  • I commented out lines of code one by one until the app would run without crashing outside the debugger. It turns out the line Style="{StaticResource IconLabelStyle}` in each of the Image tags is the source of the crash. The definition for the style is contained in the ResourceDictionaryI posted below.

        <ResourceDictionary\>  
            <Style x:Key="IconLabelStyle" TargetType="Label"\>  
                <Setter Property="FontSize" Value="Medium" /\>  
                <Setter Property="WidthRequest" Value="50" /\>  
                <Setter Property="VerticalTextAlignment" Value="Center" /\>  
                <Setter Property="HorizontalTextAlignment" Value="Center" /\>  
                <Setter Property="TextColor" Value="Black" /\>  
                <Setter Property="BackgroundColor" Value="Gold" /\>  
            </Style\>  
            <Style x:Key="LabelStyle" TargetType="Label"\>  
                <Setter Property="VerticalTextAlignment" Value="Center" /\>  
                <Setter Property="Padding" Value="5,5" /\>  
                <Setter Property="FontSize" Value="Medium" /\>  
                <Setter Property="BackgroundColor" Value="Gold" /\>  
            </Style\>  
        </ResourceDictionary\>  
    

    As you can see, The ResourceDictionary TargetType = Label, definitely not a label. Apparently the compiler isn't able to pick these things up. Would have saved me a lot of time. I added an Image style to the ResourceDisctionary, it's now:

            <ResourceDictionary>
                <Style x:Key="IconImageStyle" TargetType="Image">
                    <Setter Property="WidthRequest" Value="20" />
                    <Setter Property="HeightRequest" Value="20"/>
                    <Setter Property="VerticalOptions"  Value="Center" />
                    <Setter Property="HorizontalOptions" Value="Center" />
                    <Setter Property="BackgroundColor" Value="Gold" />
                </Style>
                <Style x:Key="IconLabelStyle" TargetType="Label">
                    <Setter Property="FontSize" Value="Medium" />
                    <Setter Property="WidthRequest" Value="50" />
                    <Setter Property="VerticalTextAlignment" Value="Center" />
                    <Setter Property="HorizontalTextAlignment" Value="Center" />
                    <Setter Property="TextColor" Value="Black" />
                    <Setter Property="BackgroundColor" Value="Gold" />
                </Style>
                <Style x:Key="LabelStyle" TargetType="Label">
                    <Setter Property="VerticalTextAlignment" Value="Center" />
                    <Setter Property="Padding" Value="5,5" />
                    <Setter Property="FontSize" Value="Medium" />
                    <Setter Property="BackgroundColor" Value="Gold" />
                </Style>
            </ResourceDictionary>