使用System.Timers和Sysem.Json的Xamarin Forms错误 [英] Xamarin Forms error using System.Timers and Sysem.Json
问题描述
我想在基于Xamarin Forms的项目中使用System.Timer.我实际上是将我的Xamarin iOS项目转换为基于Xamarin Form的项目.我的Xamarin iOS项目包含使用System.Timer的Timer的所有代码
I want to use System.Timer in my Xamarin Forms based project. I am actually converting my Xamarin iOS project to Xamarin Formbased project. My Xamarin iOS project has all code for Timer using System.Timer
aTimer = new Timer (tm);
// Hook up the Elapsed event for the timer.
aTimer.Elapsed += new ElapsedEventHandler (OnTimedEvent);
aTimer.Enabled = true;
aTimer.Start ();
当我尝试在Xamarin Forms项目中使用相同的代码时,它给出了错误.首先
When i try to use the same code in Xamarin Forms project it is giving error. First of all
using System.Timers;
它说:类型或名称空间名称Timers在名称空间系统中不存在.您是否缺少程序集参考?
It says: The type or namespace name Timers does not exist in the namespace System. Are you missing a assembly reference?
Xamarin iOS系统与Xamarin Forms System参考不同吗?
Does Xamarin iOS System different than Xamarin Forms System reference?
推荐答案
PCL项目不支持System.Timer.
PCL projects do not support System.Timer.
Xamarin Forms has a built in Timer to help workaround this limitation
Device.StartTimer (new TimeSpan (0, 0, 60), () => {
// do something every 60 seconds
return true; // runs again, or false to stop
});
如果您想通过按钮启动和停止计时器,可以执行以下操作:
If you want to start and stop the timer via buttons, you could do something like this:
bool timerStatus = false;
btnStart.Clicked += delegate {
timerStatus = true;
Device.StartTimer (new TimeSpan(h,m,x), () => {
if (timerStatus) {
// do work
}
return timerStatus;
});
};
btnStop.Clicked += delegate {
timerStatus = false;
};
这篇关于使用System.Timers和Sysem.Json的Xamarin Forms错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!