使用System.Timers和Sysem.Json的Xamarin Forms错误 [英] Xamarin Forms error using System.Timers and Sysem.Json

查看:53
本文介绍了使用System.Timers和Sysem.Json的Xamarin Forms错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在基于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具有内置的

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆