PrismLibrary / Prism.Maui

This is an experimental repo. The code here will eventually be merged into the main Prism repo as a full fledged platform at which time this repo will be archived.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Prism.Maui cannot work with ComminityToolkit's StatusBarBehavior

vulcanlee opened this issue · comments

Today, I see this Announcing the .NET MAUI Community Toolkit v1.3 blog, then I create a MAUI Project by Prism.Maui Template and upgrade Prism.DryIoc.Maui to version 8.1.273-pre.

I set this <toolkit:StatusBarBehavior StatusBarColor="Fuchsia" StatusBarStyle="LightContent" /> on <ContentPage.Behaviors>...</ContentPage.Behaviors> and run this project on Android Emulator.

I got exception : System.NotImplementedException: 'Either set MainPage or override CreateWindow.'

But when I use default .NET MAUI project template and create a project, also add <toolkit:StatusBarBehavior StatusBarColor="Fuchsia" StatusBarStyle="LightContent" /> on <ContentPage.Behaviors>...</ContentPage.Behaviors> and run this project on Android Emulator, awesome, it is working fine.

How can I use ComminityToolkit's StatusBarBehavior on Prism.Maui project and have no exception?

The following reproduce projects are on https://github.com/vulcanlee/Prism-Fail-StatusBarBehavior

Using Maui Template to create App and can work with StatusBarBehavior

  • Open Visual Studio 2022

  • New Project

  • Select [.NET MAUI Application] template

  • Set project name is mauiTemplate

  • Create this Project

  • Install [CommunityToolkit.Maui] NuGet package into this project

    the version is 1.3

  • Open [MauiProgram.cs] file, after .UseMauiApp<App>() , then add .UseMauiCommunityToolkit()

  • Open [MainPage.xaml]

    • Add new namespace : xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"
    • before <ScrollView> element, add following XAML
<ContentPage.Behaviors>
    <toolkit:StatusBarBehavior StatusBarColor="Fuchsia" StatusBarStyle="LightContent" />
</ContentPage.Behaviors>
  • Runing on Android emulator, it work fine.

Using Prism.Maui to create App and can not work with StatusBarBehavior

  • Open Visual Studio 2022

  • New Project

  • Select [Prism.NET MAUI App (Dan Siegel)] template

  • Set project name is prismTemplate

  • Create this Project

  • Install [CommunityToolkit.Maui] NuGet package into this project

    the version is 1.3

  • Upgrade [Prism.DryIoc.Maui] NuGet package to version 8.1.273-pre

  • Open [MauiProgram.cs] file, find .UsePrismApp<App>(PrismStartup.Configure) , then replace with following C#

.UseMauiApp<App>()
.UseMauiCommunityToolkit()
.UsePrism(prism=>
{
    prism.OnAppStart("MainPage");
    prism.RegisterTypes(containerRegistry =>
    {
        containerRegistry.RegisterForNavigation<MainPage>()
                        .RegisterInstance(SemanticScreenReader.Default);
    });
})
  • Open [Views] > [MainPage.xaml]
    • Add new namespace : xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"
    • before <ScrollView> element, add following XAML
<ContentPage.Behaviors>
    <toolkit:StatusBarBehavior StatusBarColor="Fuchsia" StatusBarStyle="LightContent" />
</ContentPage.Behaviors>
  • Runing on Android emulator, it got exception : System.NotImplementedException: 'Either set MainPage or override CreateWindow.'

  • I remark the <ContentPage.Behaviors> ... </ContentPage.Behaviors> XAML and re-run this project on Android emulator,

@vulcanlee thanks for the detailed bug report. This appears to be an issue with the Community Toolkit however. The default Maui template uses Shell while Prism does not. It seems the behavior isn't compatible with non-Shell apps. If I had to guess the root cause of this is that MAUI has had a bug which I'm not sure that they fixed with the proper parenting of the root page when Shell isn't used. It's also possible this could be because Shell delays the creation of the Page so you already have a Window with a Parent.

Recreating what you had from a Maui template if you simply update the App's ctor

// MainPage = new AppShell();
MainPage = new MainPage();

You'll actually see that this also fails and this is much closer to what Prism is doing with it's navigation. I would suggest opening an issue there as they can properly diagnose and fix the issue.