本文介绍了Xamarin.Forms 的 System.Threading 中不包含计时器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Xamarin.Android 中使用了 System.Threading.Timer.

如何在 Xamarin.Forms 中使用相同的类?(我想在 Xamarin.Forms 中从 Xamarin.Android 转移我的项目)

How I can use the same class in Xamarin.Forms?(I want to transfer my project from Xamarin.Android in Xamarin.Forms)

public static System.Threading.Timer timer;
if (timer == null)
{
    System.Threading.TimerCallback tcb = MyMethod;
    timer = new System.Threading.Timer(tcb, null, 700, System.Threading.Timeout.Infinite);
}
else
{
    timer.Change(System.Threading.Timeout.Infinite, System.Threading.Timeout.Infinite);
    timer.Change(700, System.Threading.Timeout.Infinite);
}

推荐答案

System.Threading.Timer 在 PCL 代码中不可用.您可以使用 Xamarin.Forms.Device.StartTimer 方法代替,如下所述:http://developer.xamarin.com/api/member/Xamarin.Forms.Device.StartTimer/

System.Threading.Timer is not available in PCL code.You can use the Xamarin.Forms.Device.StartTimer method instead as explained here:http://developer.xamarin.com/api/member/Xamarin.Forms.Device.StartTimer/

这篇关于Xamarin.Forms 的 System.Threading 中不包含计时器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 08:51