Maui Android。要么设置主页,要么覆盖CreateWindows。

huangapple go评论69阅读模式
英文:

Maui android. Either set MainPage or override CreateWindows

问题

在Maui Android应用中,当应用程序被移到后台时,偶尔会触发Destroy并将应用程序带回前台,抛出异常:

> Either set MainPage or override CreateWindow.

protected override void OnCreate(Bundle savedInstanceState)
{
    base.OnCreate(savedInstanceState); <- throws here
}

我尝试在App类中重写代码:

protected override Window CreateWindow(IActivationState activationState)
{
    return base.CreateWindow(activationState); <- throws here
}

> System.NotImplementedException: 'Either set MainPage or override CreateWindow.'

还尝试恢复先前成功加载的窗口:

private Window _window = null;

protected override Window CreateWindow(IActivationState activationState)
{
    if (MainPage == null && _window != null)
    {
        return _window;
    }

    return _window = base.CreateWindow(activationState);
}

但应用程序在此后保持无响应。尝试在AndroidManifest.xml中使用以下选项:

<manifest xmlns:android="http://schemas.android.com/apk/res/android">
    <application android:networkSecurityConfig="@xml/network_security_config" android:alwaysRetainTaskState="true" android:killAfterRestore="false" android:noHistory="false" android:excludeFromRecents="false" android:persistent="true" android:allowBackup="true" android:icon="@mipmap/appicon" android:roundIcon="@mipmap/appicon_round" android:supportsRtl="true"></application>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.CAMERA" />
</manifest>

这些选项都无法防止应用程序被销毁。StartupClass的一部分:

public static MauiApp CreateMauiApp()
{
    MauiAppBuilder builder = MauiApp.CreateBuilder();

    builder.UsePrismApp<App>(prism =>
    {
        prism.RegisterTypes(containerRegistry =>
        {
            //list of pages
            containerRegistry.RegisterForNavigation<NavigationPage>();
            ....
        })
        .OnAppStart(async (containerProvider, navigationService) =>
        {
            var navService = containerProvider.Resolve<INavigationService>();
            await navService.NavigateAsync(nameof(MyPage));
        });

在这里,我在Maui上使用了Prism。是否有任何方法可以防止应用程序被销毁,或者在重新创建时成功恢复应用程序状态?

英文:

In maui android app, when app was moved to back it occassionally invokes Destroy and bringing app back to front, throws an exeption:

> Either set MainPage or override CreateWindows.

 protected override void OnCreate(Bundle savedInstanceState)
 {
    base.OnCreate(savedInstanceState); <- throws here
 }

i tried to override code in App class:

 protected override Window CreateWindow(IActivationState activationState)
 {
    return base.CreateWindow(activationState); <- throws here
 }

> System.NotImplementedException: 'Either set MainPage or override
> CreateWindow.'

also tried to restore previously successfully loaded window:

 private Window _window = null;

protected override Window CreateWindow(IActivationState activationState)
{
    if (MainPage == null && _window != null)
    {
        return _window;
    }

    return _window = base.CreateWindow(activationState);
}

but app stays unresponsive after it.
Tried to use in AndroidManifest.xml options:

<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<application android:networkSecurityConfig="@xml/network_security_config" android:alwaysRetainTaskState="true" android:killAfterRestore="false" android:noHistory="false" android:excludeFromRecents="false" android:persistent="true" android:allowBackup="true" android:icon="@mipmap/appicon" android:roundIcon="@mipmap/appicon_round" android:supportsRtl="true"></application>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CAMERA" />

</manifest>
none of that had a way to prevent from destroying app.
Part of my StartupClass:

    public static MauiApp CreateMauiApp()
{
    MauiAppBuilder builder = MauiApp.CreateBuilder();

    builder.UsePrismApp&lt;App&gt;(prism =&gt;
    {
        prism.RegisterTypes(containerRegistry =&gt;
        {
            //list of pages
            containerRegistry.RegisterForNavigation&lt;NavigationPage&gt;();
            ....
        })
        .OnAppStart(async (containerProvider, navigationService) =&gt;
        {
            var navService = containerProvider.Resolve&lt;INavigationService&gt;();
            await navService.NavigateAsync(nameof(MyPage));
        });

here i use prism over maui.
So is there any way to prevent app from destroying or to successfully restore app state on recreate?

答案1

得分: 1

关于覆盖 CreateWindows,您可以尝试使用以下代码:

protected override Window CreateWindow(IActivationState activationState)
{
    if (this.MainPage == null)
    {
        this.MainPage = new MainPage();
    }

    return base.CreateWindow(activationState);
}

有关更多信息,您可以参考此问题:Android 应用从后台启动时崩溃。希望可以帮助您。

英文:

> Maui android. Either set MainPage or override CreateWindows

About override CreateWindows, you can try to use following code:

protected override Window CreateWindow(IActivationState activationState)
{
    if (this.MainPage == null)
    {
        this.MainPage = new MainPage();
    }

    return base.CreateWindow(activationState);
}

For more info you can refer to this issue: [Android] App crashes when start from background. Wish it can help you.

答案2

得分: 1

我最近也遇到了同样的问题。我正在使用Shiny.Framework。但是即使是Shiny.Framework提供的示例也会引发异常。

我查找了更多信息,这个评论来自maui仓库,显示这可能是模拟器的问题。所以我创建了一个新的模拟器,使用了最新的Android 33镜像,问题就消失了。

我目前发现,与VS 2022一起提供的默认镜像是Android 29,总是会引发异常。我尝试了一下,似乎Android版本31以上的镜像都可以正常工作。

另外:

我遇到的异常也有这个消息:

> (示例) ChildIndex <= -1,父级:App 子级:PrismWindow

所以我认为这与Prism有关。但我不是Prism或Shiny的专家,所以无法深入挖掘。希望有人能解决这个谜团。

英文:

I encountered the same problem myself lately. I'm using Shiny.Framework. But even the sample provided by Shiny.Framework throws the exception.

I googled some more, this comment from maui repo shows me maybe this is an emulator problem. So I created a new emulator using the latest Android 33 image, and the problem went away.

I currently find that the default iamge comes with VS 2022, which is Android 29, will always throw the exception. I tried, it seems that images above android v31 works fine.

And PS:

The exception I have also had this message:

> (Sample) ChildIndex <= -1, Parent: App Child: PrismWindow

So I think it has something to do with Prism. But I'm no expert on Prism or Shiny, so I can't dig any deeper. Hoper some one can solve this mystery.

huangapple
  • 本文由 发表于 2023年5月17日 22:37:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76273297.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定