首页 > 解决方案 > 跨平台 Xamarin 表单应用程序 - 未显示 Android 启动画面

问题描述

我正在创建我的第一个 Xamarin Forms 跨平台应用程序。我正在使用每个操作系统的本机平台代码创建启动画面。

我的 Android 启动画面未显示。我跟踪应用程序并找到正确的代码,但页面不显示。

我的 xml 文件位于 resources\layout 文件夹中,当我查看它时效果很好。它被称为 SplashScreen.xml。

谁能解释为什么页面不显示?

{
    [Activity(MainLauncher = true, NoHistory = true)]
    public class SplashScreenActivity : Activity
    {
        protected override void OnCreate(Bundle savedInstanceState)
        {
            RequestWindowFeature(WindowFeatures.NoTitle);

            base.OnCreate(savedInstanceState);

            SetContentView(Droid.Resource.Layout.SplashScreen);

            System.Threading.Thread.Sleep(3000);
            StartActivity(typeof(MainActivity));


        }

        public override void OnBackPressed() { }
     }
}


标签: xamarin.forms

解决方案


您应该首先编写一个主题来加载您的 xml,如下所示:

<resources>
  <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/SplashScreen</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>

然后在活动中使用该主题:

[Activity(Theme = "@style/MyTheme.Splash", MainLauncher = true, NoHistory = true)]

是有关 Xamarin.Android 中的启动画面的文档,您可以遵循详细的步骤。

还提供了一个示例项目:splashscreen


推荐阅读