Xamarin.forms Splashscreen(Android)之后出现白屏闪烁

时间:2020-05-04 15:44:13

标签: xamarin xamarin.forms xamarin.android

这个问题已经在SO和xamarin.forums上问了好几次了。但是我无法解决这个问题。

在我的xamarin.forms android应用中,我有一个启动画面,它可以完美启动。问题在于加载我的主页和启动屏幕之间,将出现白屏闪烁。我遵循了https://xamarinhelp.com/creating-splash-screen-xamarin-forms/中的教程。

我通过为启动画面创建特定活动并直接将启动主题设置为MainActivity来尝试了。无论是哪种方式,我都无法摆脱白屏闪烁的问题。我在释放模式下尝试了一下,问题仍然存在。使用xamarin是预期的行为吗?我该如何删除?任何帮助表示赞赏。

我的MainActivty

   [Activity(Label = "SampleApp", Icon = "@mipmap/ic_launcher", Theme = "@style/splashscreen", ScreenOrientation = ScreenOrientation.Portrait, MainLauncher = true, LaunchMode = LaunchMode.SingleTop, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
    public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
    {
        static readonly string TAG = "MainActivity";
        protected override void OnCreate(Bundle savedInstanceState)
        {
            TabLayoutResource = Resource.Layout.Tabbar;
            ToolbarResource = Resource.Layout.Toolbar;           
            global::Xamarin.Forms.Forms.SetFlags("CollectionView_Experimental");
            base.SetTheme(Resource.Style.MainTheme);
            base.OnCreate(savedInstanceState);

        }           
}

我在MainActivity中有几个库Init(),这里没有包括。

我的App.Xaml.cs

 public partial class App : Application
    {

        public App()
        { 
                var Login = new NavigationPage(new LoginPage());
                MainPage = Login

        }
        protected override void OnStart()
        {            
        }

        protected override void OnSleep()
        {
            // Handle when your app sleeps
        }

        protected override void OnResume()
        {
            // Handle when your app resumes
        }
    }

我的风格

<style name="splashscreen" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:windowBackground">@drawable/Splash</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowIsTranslucent">false</item>
    <item name="android:windowIsFloating">false</item>
    <item name="android:backgroundDimEnabled">true</item>  
  </style>

  <style name="MainTheme" parent="MainTheme.Base">
  </style>
  <!-- Base theme applied no matter what API -->
  <style name="MainTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
    <!--If you are using revision 22.1 please use just windowNoTitle. Without android:-->
    <item name="windowNoTitle">true</item>


    <!--We will be using the toolbar so no need to show ActionBar-->
    <item name="windowActionBar">false</item>
    <!-- Set theme colors from http://www.google.com/design/spec/style/color.html#color-color-palette -->
    <!-- colorPrimary is used for the default action bar background -->
    <item name="colorPrimary">#2196F3</item>
    <!-- colorPrimaryDark is used for the status bar -->
    <item name="colorPrimaryDark">#1976D2</item>
     <item name="android:backgroundDimEnabled">true</item>
    <!-- colorAccent is used as the default value for colorControlActivated
         which is used to tint widgets -->
    <item name="colorAccent">#FF4081</item>

    <item name="android:textAllCaps">false</item>
    <!-- You can also set colorControlNormal, colorControlActivated
         colorControlHighlight and colorSwitchThumbNormal. -->
    <item name="windowActionModeOverlay">true</item>

    <item name="android:datePickerDialogTheme">@style/AppCompatDialogStyle</item>
  </style>

  <style name="AppCompatDialogStyle" parent="Theme.AppCompat.Light.Dialog">
    <item name="colorAccent">#FF4081</item>
  </style>

我的可绘制/飞溅

<?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/Logo"
        android:tileMode="disabled"
        android:gravity="center"/>
  </item>
</layer-list>

2 个答案:

答案 0 :(得分:3)

尝试将其添加到您的样式中:

<item name="android:windowAnimationStyle">@null</item>

赞:

<style name="splashscreen" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:windowBackground">@drawable/Splash</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowIsTranslucent">false</item>
    <item name="android:windowIsFloating">false</item>
    <item name="android:backgroundDimEnabled">true</item>
    <item name="android:windowAnimationStyle">@null</item> //Add this line
</style>

我建议使用SplashActivity.cs并添加

AddFlags(ActivityFlags.NoAnimation); 

赞:

[Activity(
    Icon = "@mipmap/icon",
    RoundIcon = "@mipmap/icon_round",
    Theme = "@style/SplashTheme",
    MainLauncher = true,
    NoHistory = true,
    ScreenOrientation = ScreenOrientation.Portrait,
    LaunchMode = LaunchMode.SingleTop)]
public class SplashActivity : AppCompatActivity
{
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);

        if (Build.VERSION.SdkInt >= BuildVersionCodes.Lollipop)
        {
            Window.DecorView.SystemUiVisibility = StatusBarVisibility.Visible;
            Window.SetStatusBarColor(Android.Graphics.Color.Transparent);
        }

        InvokeMainActivity();
    }

    private void InvokeMainActivity()
    {
        var mainActivityIntent = new Intent(this, typeof(MainActivity));
        mainActivityIntent.AddFlags(ActivityFlags.NoAnimation); //Add this line
        StartActivity(mainActivityIntent);
    }
}

答案 1 :(得分:0)

问题出在NavigationPage对象上

用shell替换所有导航,它将被修复。 Shell似乎还可以更好地处理过渡动画,因此很棒。