微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

启动画面在每个页面上显示为背景

如何解决启动画面在每个页面上显示为背景

我的启动屏幕显示在开始处,但仍保持运行状态,好像它是页面的背景一样。我必须使用BackgroundColor,以便背景不可见,但任何过渡都可以非常短暂地显示背景。

这是我的MainActivity.cs

using System;
using Android.App;
using Android.Content.PM;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;

namespace apptest.Droid
{
    [Activity(Label = "apptest",Icon = "@drawable/appmain",Theme = "@style/MyTheme.Splash",NoHistory = true,ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
    public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
    {
        protected override void OnCreate(Bundle savedInstanceState)
        {
            TabLayoutResource = Resource.Layout.Tabbar;
            ToolbarResource = Resource.Layout.Toolbar;

           
            base.OnCreate(savedInstanceState);
            Xamarin.Essentials.Platform.Init(this,savedInstanceState);
            global::Xamarin.Forms.Forms.Init(this,savedInstanceState);
            LoadApplication(new App());
        }
        public override void OnRequestPermissionsResult(int requestCode,string[] permissions,[GeneratedEnum] Android.Content.PM.Permission[] grantResults)
        {
            Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode,permissions,grantResults);

            base.OnRequestPermissionsResult(requestCode,grantResults);
        }
    }
}

这是我的SplashActivity.cs

using System.Threading.Tasks;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Support.V7.App;
using Android.Util;

namespace apptest.Droid
{
    [Activity(Theme = "@style/MyTheme.Splash",MainLauncher = true,NoHistory = true)]
    public class SplashActivity : AppCompatActivity
    {
        static readonly string TAG = "X:" + typeof (SplashActivity).Name;

        public override void OnCreate(Bundle savedInstanceState,PersistableBundle persistentState)
        {
            base.OnCreate(savedInstanceState,persistentState);
            Log.Debug(TAG,"SplashActivity.OnCreate");
        }

        // Launches the startup task
        protected override void OnResume()
        {
            base.OnResume();
            Task startupWork = new Task(() => { SimulateStartup(); });
            startupWork.Start();
        }

        // Prevent the back button from canceling the startup process
        public override void OnBackpressed() { }

        // Simulates background work that happens behind the splash screen
        async void SimulateStartup ()
        {
            Log.Debug(TAG,"Performing some startup work that takes a bit of time.");
            await Task.Delay(8000); // Simulate a bit of startup work.
            Log.Debug(TAG,"Startup work is finished - starting MainActivity.");
            StartActivity(new Intent(Application.Context,typeof (MainActivity)));
        }
    }
}

有没有办法使启动画面消失?

请,谢谢! :)

解决方法

就像David所说的那样,不要为MainActivity设置Splash主题。您可以使用项目创建的默认主题。

 [Activity(Label = "SplashScreen",Icon = "@mipmap/icon",Theme = "@style/MainTheme",NoHistory = true,ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]

截屏:

enter image description here

您可以从Github下载源文件以供参考。 https://github.com/WendyZang/Test/tree/master/SplashScreen

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。