簡體   English   中英

Xamarin-Android-啟動畫面(多個)

[英]Xamarin - Android - Splash Screen (Multiple)

我剛剛開始使用xamarin創建我的第一個android應用程序,但是我遇到了一個小問題。

我想在應用程序的開頭有兩個啟動屏幕。

我創建了第一個正常工作的,然后創建了第二個,然后是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++);
            };
        }
    }

問題在於,因為您正在使用Thread.Sleep(3000)使UI線程處於休眠狀態,所以UI會凍結,並且新的Activity將在OnCreate方法返回之前啟動。

我建議使用計時器(例如: 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++);
            };
        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM