Xamarin android c#在3秒后调用或显示活动

时间:2016-05-15 13:53:24

标签: c# xamarin xamarin.android

我想在3秒后打电话给一个活动。我试过使用线程,但它没有工作......

[Activity(Label = "App1", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity
{


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

        // Set our view from the "main" layout resource
        SetContentView(Resource.Layout.Login);
        Button myButton = FindViewById<Button>(Resource.Id.button4);

        myButton.Click += delegate
        {

            StartActivity(typeof(Register));
        };


        new Handler().postDelayed(new Runnable()
    {
        public void run()
    {
        //After 3 second will call this activity
        StartActivity(typeof(MainActivity));
    }
    }, 5000);


    }    

}

2 个答案:

答案 0 :(得分:3)

    myButton.Click += async delegate
    {
        await Task.Delay(3000);
        StartActivity(typeof(Register));
    };

使用lambda

的语法
myButton.Click += async (sender, args) =>
{
     await Task.Delay(3000);
     StartActivity(typeof (Register));
};

答案 1 :(得分:2)

您也可以使用

 Java.Lang.Runnable runnable = new Java.Lang.Runnable(() =>
        {
            Intent i = new Intent(this, typeof(MainActivity));
            StartActivity(i);
        });

        new Handler().PostDelayed(runnable, 1000);