Xamarin 中的启动画面太慢 Android

Splash screen too slow in Xamarin Android

我正在创建一个显示初始屏幕然后创建主屏幕的应用 activity。我正在学习这个看起来非常简单的教程:https://developer.xamarin.com/guides/android/user_interface/creating_a_splash_screen/

实施后,我可以成功看到启动画面,但有时(20 次中有 1 次)使用 S5 我会看到以下屏幕:

后面是(右)启动画面(取自模拟器,但只是为了说明我的观点):

所以我的猜测是,有时 Xamarin 需要很长时间才能加载应用程序,因此它会延迟显示启动画面。有什么办法可以避免吗?

更新 1 我已经按照教程进行操作,但我已经为此删除了睡眠:

Insights.Initialize ("<APP_KEY>", Application.Context);
StartActivity(typeof (MainActivity));

该示例在 UI 线程上调用 Thread.Sleep(10000);... 这将锁定应用程序并生成 ANR!

通过后台睡眠然后触发下一个 activity:

来修复它
namespace SplashScreen
{
    using System.Threading;

    using Android.App;
    using Android.OS;

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

            Task.Run (() => {
                Thread.Sleep (10000); // Simulate a long loading process on app startup.
                RunOnUiThread (() => { 
                    StartActivity (typeof(Activity1));
                });
            });
        }
    }
}

即使这个 post 相当旧,我在实现 SplashScreen 时也有类似的经历,可以通过更新 SplashScreen 的 style/theme 来解决这个问题。 @frederico m rinaldi 有时看到的屏幕通常是使用 Android 的默认 (Holo) 主题创建的。

虽然您没有提供应用于启动画面的样式(请参阅 Theme = @style/Theme.Splash),但这是我的。也许你可以检查它们是否不同。

<style name="Theme.Splash" parent ="Theme.AppCompat.Light.NoActionBar">
  <!-- Use a fully opaque color as background. -->
  <item name="android:windowBackground">@android:color/black</item>
  <!-- This removes the title bar seen within the first screen. -->
  <item name="windowNoTitle">true</item>
  <!-- Let the splash screen use the entire screen space by enabling full screen mode -->
  <item name="android:windowFullscreen">true</item>
  <!-- Hide the ActionBar (Might be already defined within the parent theme) -->
  <item name="windowActionBar">false</item>
</style>

您可能会注意到我只使用黑色作为背景,因为我的 SplashScreen 使用自定义布局文件而不是静态图像 (this.SetContentView(Resource.Layout.SplashScreen);)。此外,加载图像 (drawable) 可能需要一些时间,这可能是您看到默认主题而不是启动画面的主要原因。 此外,由于 Android 支持库功能的 Google's internal implementation,我省略了某些属性的 android: XML 命名空间。

请注意,为了使用 AppCompat 主题,您的应用程序必须包含 AppCompat support library 并且您的 Activity 必须是 Android.Support.V7.App.AppCompatActivity.

的子类