15

I'm trying to show some code after a delay in my Android app.
The Java code for doing this is something like this:

new Handler().postDelayed(new Runnable()
{
   @Override
   public void run()
   {
     // your code that you want to delay here
   }
}, 1000/* 1000ms = 1sec delay */);

How do I do this in Xamarin.Android with C#?

4

2 回答 2

31

你可以试试这个:

Handler h = new Handler();
Action myAction = () => 
{
    // your code that you want to delay here
};

h.PostDelayed(myAction, 1000);

看看文档

于 2016-08-28T07:48:29.343 回答
1

我建议您使用像AdvancedTimer这样的跨平台计时器。检查:github repo

API 使用

要访问 Timer 类,只需使用依赖服务:

IAdvancedTimer timer = DependencyService.Get<IAdvancedTimer>();

您必须调用 initTimer 进行定时器初始化;

timer.initTimer(3000, timerElapsed, true);

initTimer(interval, Eventhandler function, AutoReset);

方法

timer.startTimer();

timer.stopTimer();

timer.getInterval()

timer.setInterval(5000);

timer.isTimerEnabled();
于 2016-08-28T08:02:58.207 回答