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

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

问题描述

我想在基于 Xamarin Forms 的项目中使用 System.Timer.我实际上是将我的 Xamarin iOS 项目转换为 Xamarin Formbased 项目.我的 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 System 与 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天全站免登陆