【问题标题】:Xamarin.forms A flash of white screen after Splashscreen (Android)Xamarin.forms Splashscreen (Android) 后闪现的白屏
【发布时间】:2020-05-04 15:44:13
【问题描述】:

这个问题已在 SO 和 xamarin.forums 上多次提出。但我没能解决这个问题。

在我的 xamarin.forms android 应用程序中,我有一个启动画面。它可以完美启动。问题是在加载我的主页和启动屏幕之间,会出现一闪而过的白屏。我按照https://xamarinhelp.com/creating-splash-screen-xamarin-forms/ 的教程进行操作。

我尝试通过为启动画面创建特定活动并将启动主题直接设置为 MainActivity。在这两种方法中我都无法摆脱白屏闪烁。我在发布模式下尝试过,问题仍然存在。这是使用 xamarin 的预期行为吗?我怎样才能删除它?感谢您提供任何帮助。

我的 MainActivity

   [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>

我的drawable/Splash

<?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>

【问题讨论】:

  • 你能分享一个带有闪烁的gif/视频吗?我过去也遵循过本教程,但没有看到任何闪烁。另外,能分享一下闪屏代码吗?
  • @MihailDuchev 嗨,我没有使用闪屏活动。我只是在 mainactvity 中设置启动主题并使用 base.SetTheme(Resource.Style.MainTheme); 将主题替换为 MainTheme;
  • @MihailDuchev 早些时候我尝试过使用 Splash 活动,但它对这个问题没有太大影响
  • 你应该在这里添加splashscreen样式
  • @Prateek 我添加了样式

标签: xamarin xamarin.forms xamarin.android


【解决方案1】:

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

<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);
    }
}

【讨论】:

  • 感谢您的回答。让我看看威尔逊
  • 嗨,我试过你的代码。现在它会卡在启动画面上。不导航到主页
  • 您应该从 MainActivity 中删除 base.SetTheme(Resource.Style.MainTheme); 并将 Theme = "@style/splashscreen" 更改为 Theme = "@style/MainTheme"
  • 威尔逊。同样的问题仍然存在
  • 我认为原因是由于 Maintheme 背景的背景颜色。它是白色的。我们可以在这部分有任何解决方法吗?
【解决方案2】:

问题在于 NavigationPage 对象

用shell替换所有导航,它将被修复。 Shell 似乎也能更好地处理过渡动画,这很棒。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-05
    • 1970-01-01
    • 1970-01-01
    • 2013-02-10
    相关资源
    最近更新 更多