Odd startup error in ported dotnet 9 Maui Android app

22 hours ago 3
ARTICLE AD BOX

I migrated a Xamarin Forms android app to Maui (.net 9), and it mostly works well. I have to admit it was easier than I expected. The XF app loaded pages by setting MainPage, and the app manages its own page stack. I am trying to move the Maui app to Shell navigation, but I'm hitting a roadblock.

The app starts out with a splash page that is instantiated in the App ctor (App.xaml.cs) like so

`MainPage = new SplashPage();`

There is also view model SplashPageViewModel, identified in SplashPage.xaml like so:

<?xml version="1.0" encoding="utf-8" ?> <ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="CryptoVault.Views.SplashPage" xmlns:local="clr-namespace:CryptoVault.ViewModels;assembly=CryptoVaultAndroidApp" xmlns:vm="clr-namespace:CryptoVault.ViewModels" x:DataType="vm:SplashPageViewModel" > <ContentPage.BindingContext> <local:SplashPageViewModel/> </ContentPage.BindingContext>

When I comment out the MainPage = new SplashPage() line of code in App.xaml.cs, and override CreateWindow like so:

protected override Window CreateWindow(IActivationState? activationState) { return new Window(new AppShell()); }

and have AppShell.xaml like so:

<ShellContent Title="Home" ContentTemplate="{DataTemplate local:MauiProgram}" Route="SplashPage" />

I get this error during startup in "external code":

System.InvalidOperationException Message=Instances of abstract classes cannot be created.

With this stack trace:

0xFFFFFFFFFFFFFFFF in Android.Runtime.RuntimeNativeMethods.monodroid_debugger_unhandled_exception C# 0x1A in Android.Runtime.JNINativeWrapper._unhandled_exception at /Users/runner/work/1/s/Xamarin-android/src/Mono.Android/Android.Runtime/JNINativeWrapper.g.cs:12,5 C# 0x23 in Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PPLLL_L at /Users/runner/work/1/s/Xamarin-android/src/Mono.Android/Android.Runtime/JNINativeWrapper.g.cs:364,26 C# 0x20 in Microsoft.Extensions.DependencyInjection.ActivatorUtilities.CreateInstance C# 0x68 in Microsoft.Maui.Controls.ShellContent. at /_/src/Controls/src/Core/Shell/ShellContent.cs:87,7 C# 0x27 in Microsoft.Maui.Controls.ElementTemplate.CreateContent at /_/src/Controls/src/Core/ElementTemplate.cs:87,4 C# 0x8 in Microsoft.Maui.Controls.Internals.DataTemplateExtensions.CreateContent at /_/src/Controls/src/Core/DataTemplateExtensions.cs:23,4 C# 0x71 in Microsoft.Maui.Controls.ShellContent.Microsoft.Maui.Controls.IShellContentController.GetOrCreateContent at /_/src/Controls/src/Core/Shell/ShellContent.cs:91,5 C# 0x16E in Microsoft.Maui.Controls.Platform.Compatibility.ShellSectionRenderer.OnCreateView at /_/src/Controls/src/Core/Compatibility/Handlers/Shell/Android/ShellSectionRenderer.cs:124,5 C# 0x24 in AndroidX.Fragment.App.Fragment.n_OnCreateView_Landroid_view_LayoutInflater_Landroid_view_ViewGroup_Landroid_os_Bundle_ at C:\a\_work\1\s\generated\androidx.fragment.fragment\obj\Release\net8.0-android\generated\src\AndroidX.Fragment.App.Fragment.cs:2045,4 C# 0xD in Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PPLLL_L at /Users/runner/work/1/s/Xamarin-android/src/Mono.Android/Android.Runtime/JNINativeWrapper.g.cs:363,5 C#

I must be missing something basic ... what I'm doing seems to follow the same startup pattern as a new "do nothing" Maui app, but something is clearly not right. If anyone can point me in the right direction, I would appreciate it.


== UPDATE ==

Problem resolved!

The problem was in AppShell.xaml. The issue disappeared when I changed the DataTemplate from MauiProgram to SplashPage, like so:

<ShellContent Title="Home" ContentTemplate="{DataTemplate local:MauiProgram}" Route="SplashPage" />

to

<ShellContent Title="Home" ContentTemplate="{DataTemplate local:SplashPage}" Route="SplashPage" />

The problem was always there in the original, but it only mattered when (that is, the code was not being executed until) I switched from navigating to the page by setting MainPage, to navigation by overriding CreateWindow and instantiating AppShell.

Thanks!

Read Entire Article