Xamarin - Android - 启动画面(多个)

时间:2015-01-17 18:05:37

标签: android xamarin splash-screen

我刚刚开始使用xamarin创建我的第一个Android应用程序而且我有一个小问题。

我希望在应用的开头有2个启动画面。

我创建了第一个工作正常,然后是第二个然后是mainActivity。

但出于某种原因,它并没有显示第二个闪屏。

如果我在第二行中删除了这一行,那么它可以工作,但不会转到mainActivity。

StartActivity(typeof(MainActivity));

MainActivity.cs

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

            Thread.Sleep(3000); 

            StartActivity(typeof(SplashActivity2));
        }
    };

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

            Thread.Sleep(3000); 

            StartActivity(typeof(MainActivity));
        }
    };

    [Activity (Label = "coco1_droid", Icon = "@drawable/icon")]
    public class MainActivity : Activity
    {
        int count = 1;

        protected override void OnCreate (Bundle bundle)
        {
            base.OnCreate (bundle);

            // Set our view from the "main" layout resource
            SetContentView (Resource.Layout.Main);

            // Get our button from the layout resource,
            // and attach an event to it
            Button button = FindViewById<Button> (Resource.Id.myButton);

            button.Click += delegate {
                button.Text = string.Format ("{0} clicks!", count++);
            };
        }
    }

1 个答案:

答案 0 :(得分:1)

问题在于,因为您正在使用Thread.Sleep(3000)暂停UI线程,所以UI会冻结,并且在OnCreate方法返回之前会启动新的Activity。

我建议使用计时器(例如:System.Timers.Timer)在开始新活动前等待三秒钟。这样UI就不会冻结,OnCreate方法会返回。

我已根据我的建议修改了您的示例:

MainActivity.cs

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

            Timer timer = new Timer();
            timer.Interval = 3000; // 3 sec.
            timer.AutoReset = false; // Do not reset the timer after it's elapsed
            timer.Elapsed += (object sender, ElapsedEventArgs e) =>
            {
                StartActivity(typeof(SplashActivity2));
            };
            timer.Start();
        }
    };

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

            Timer timer = new Timer();
            timer.Interval = 3000; // 3 sec.
            timer.AutoReset = false; // Do not reset the timer after it's elapsed
            timer.Elapsed += (object sender, ElapsedEventArgs e) =>
            {
                StartActivity(typeof(MainActivity));
            };
            timer.Start();
        }
    };

    [Activity (Label = "coco1_droid", Icon = "@drawable/icon")]
    public class MainActivity : Activity
    {
        int count = 1;

        protected override void OnCreate (Bundle bundle)
        {
            base.OnCreate (bundle);

            // Set our view from the "main" layout resource
            SetContentView (Resource.Layout.Main);

            // Get our button from the layout resource,
            // and attach an event to it
            Button button = FindViewById<Button> (Resource.Id.myButton);

            button.Click += delegate {
                button.Text = string.Format ("{0} clicks!", count++);
            };
        }
    }