android 毛伊岛机器人设置MainPage或覆盖CreateWindows

gblwokeq  于 2023-05-21  发布在  Android
关注(0)|答案(1)|浏览(208)

在maui android应用程序中,当应用程序被移到后面时,它偶尔会调用Destroy并将应用程序带回到前面,抛出一个exeption:
设置MainPage或覆盖CreateWindows。

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:'设置MainPage或重写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" />

这些都没有办法防止破坏应用程序。我的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));
        });

在这里我使用prism over maui。那么,有没有什么方法可以防止应用程序被破坏或在重新创建时成功恢复应用程序状态呢?

wbgh16ku

wbgh16ku1#

毛伊岛机器人设置MainPage或覆盖CreateWindows
关于override CreateWindows,可以尝试使用以下代码:

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

    return base.CreateWindow(activationState);
}

有关更多信息,您可以参考此问题:[Android] App crashes when start from background .希望它能帮助你。

相关问题