启动画面后的白屏

White screen after splash screen

当我将启动画面放入我的应用程序时,出现白屏,然后是主屏幕。

我发现是白屏我的应用程序显示了两秒然后进入主屏幕。

我想知道是否有办法删除此页面或使其持续零秒?

感谢回答我的人!

这段代码是我的启动画面:

[Activity(Label = "myapp", MainLauncher = true, Theme = "@style/Theme.Splash", NoHistory = true)]
    public class SplashActivity : Activity
    {
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            // Create your application here
        }


        protected override void OnResume()
        {
            base.OnResume();
            Task splashwork = new Task(async () => { await Task.Delay(3000); });
            splashwork.ContinueWith(h =>
            {
                StartActivity(new Intent(Application.Context, typeof(MainActivity)));

                TaskScheduler.FromCurrentSynchronizationContext();
            });
            splashwork.Start();
        }
    }

这是针对 Android 10 的 Xamarin Forms 的一个已知问题,同时使用 NavigationPage 作为主页的包装器,XF 开发团队仍未修复您可以关注它 progress/status on [Bug] 使用 NavigationPage 时 Android 中的白屏

你的任务延迟显示“3000”,这意味着它需要 3 秒,降低到“1000”,看看是否有帮助

您可以使用两个活动。一个 activity 用于显示启动画面,另一个用于显示 mian activity.

你可以看看我之前做的示例代码。

更新:

创建启动画面。 https://docs.microsoft.com/en-us/xamarin/android/user-interface/splash-screen

在 drawable 文件夹中创建 splash_screen.xml。

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
 <item>
   <color android:color="@color/splash_background"/>
 </item>
  <item>
  <bitmap
    android:src="@drawable/splash_logo"
    android:tileMode="disabled"
    android:gravity="center"/>
  </item>
</layer-list>

在Resources/values/colors.xml.

中添加特殊颜色
<?xml version="1.0" encoding="utf-8"?>
<resources>
  ...
  <color name="splash_background">#FFFFFF</color>
</resources>

在 Resources/values/styles 中创建主题。xml

 <style name="Theme_SplashScreen" parent ="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowBackground">@drawable/splash_screen</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowActionBar">true</item>

在 Activity_SplashScreen activity 中加载启动画面。

 [Activity(Label = "Activity_SplashScreen", Theme = "@style/Theme_SplashScreen", MainLauncher = true)]
public class Activity_SplashScreen : Activity
{
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);

        // Create your application here
    }
    protected override void OnResume()
    {
        base.OnResume();
        Task startupWork = new Task(() => { SimulateStartup(); });
        startupWork.Start();
    }
    // Simulates background work that happens behind the splash screen
    async void SimulateStartup()
    {             
        await Task.Delay(3000); // Simulate a bit of startup work.            
        StartActivity(new Intent(Application.Context, typeof(Activity_Screen)));
    }
}

Activity_Screen activity:

 [Activity(Label = "Activity_Screen")]
public class Activity_Screen : Activity
{
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);

        // Create your application here
        SetContentView(Resource.Layout.layout_Screen);
        Toast.MakeText(this, "Welcome to MainActivity", ToastLength.Long).Show();
    }
}

在你的情况下可能没有解决方案,但在我的情况下它有效。

我刚刚创建了一个干净的 Xamarin 应用程序,并使用 Microsoft 网站上的步骤向其添加了启动画面。

Xamarin SplashScreen

它可以工作,但在启动画面之后和应用程序加载之前出现黑屏(有时需要几秒钟)。

按照 MSDN 中的已回答问题 Xamarin Forms 10 seconds black screen after Splash Screen

解决方案是将 <item name="android:windowIsTranslucent">true</item> 添加到主 activity 主题的样式中。然后我的 style.xml 文件这样结束:

<?xml version="1.0" encoding="utf-8" ?>
<resources>
  <style name="MainTheme" parent="MainTheme.Base">    
    <item name="android:windowIsTranslucent">true</item> <!--Add this line-->
  </style>

    <style name="MyTheme.Base" parent="Theme.AppCompat.Light">
    </style>

    <style name="MyTheme" parent="MyTheme.Base">
    </style>

    <style name="MyTheme.Splash" parent ="Theme.AppCompat.Light.NoActionBar">
        <item name="android:windowBackground">@drawable/splash_screen</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowFullscreen">true</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:windowActionBar">true</item>
    </style>
</resources>