C# 定时器不工作';Xamarin.Forms处的System.Threading中不包含

C# 定时器不工作';Xamarin.Forms处的System.Threading中不包含,c#,timer,xamarin,xamarin.android,xamarin.forms,C#,Timer,Xamarin,Xamarin.android,Xamarin.forms,我在Xamarin.Android中使用了System.Threading.Timer 如何在Xamarin.Forms中使用相同的类? (我想在Xamarin.Forms中从Xamarin.Android转移我的项目) System.Threading.Timer在PCL代码中不可用。 您可以使用Xamarin.Forms.Device.StartTimer方法,如下所述: 对于PCL,您可以使用异步/等待功能创建自己的PCL。这种方法的另一个优点是,计时器方法实现可以在计时器处理程序中的异步

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

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


System.Threading.Timer
在PCL代码中不可用。 您可以使用
Xamarin.Forms.Device.StartTimer
方法,如下所述:
对于PCL,您可以使用异步/等待功能创建自己的PCL。这种方法的另一个优点是,计时器方法实现可以在计时器处理程序中的异步方法上等待

public sealed class AsyncTimer : CancellationTokenSource
{
    public AsyncTimer (Func<Task> callback, int millisecondsDueTime, int millisecondsPeriod)
    {
        Task.Run(async () =>
        {
            await Task.Delay(millisecondsDueTime, Token);
            while (!IsCancellationRequested)
            {
                await callback();
                if (!IsCancellationRequested)
                    await Task.Delay(millisecondsPeriod, Token).ConfigureAwait(false);
            }
        });
    }

    protected override void Dispose(bool disposing)
    {
        if (disposing)
            Cancel();

        base.Dispose(disposing);
    }
}

嗨,我在Xamarin.forms中找到了计时器的解决方案

  • Device.StartTimer(TimeSpan.frommissions(1000),OnTimerTick)
    //TimeSpan.FromMillistics(1000)以毫秒为单位指定时间
    //OnTimerTick将执行返回布尔值的函数

  • private bool OnTimerTick() {//要执行的代码 lblTime.Text=newHighScore.ToString(); newHighScore++; 返回true; }

  • 我希望你很容易明白我的意思
    谢谢

    你试过了吗?没有。我有一个关于Xamarin.Android的项目。此项目使用System.Threading.Timer。我需要将这个项目从Xamarin.Android转移到Xamarin.FormsAm,我想你是对的,你只是在寻找一个方法的延迟执行,而没有周期性的方法调用?我也使用TimerCallback和Change method,你把一个计时器回调作为第二个参数传递。它是一个返回
    bool
    的方法。当您想再次执行回调时返回
    true
    ,或者当您想停止回调时返回
    false
    。您可以在这里找到文档:现在
    System.Threading.Timer
    在Xamarin.Forms中提供,如果您的目标是Netstandard2我如何使用TimerCallback和Change方法?这是一个很好的解决方案。一个问题:当我不再需要计时器时,如何停止它?它继承自CancellationToken,因此您可以对timer对象执行.Cancel()方法。它可以工作,因为它在内部检查IsCancellationRequested,并将此令牌传递给Task.Delay()调用。但是,您自己的回调函数将不会接收此令牌。但您可以更改代码以添加此功能。
    public sealed class AsyncTimer : CancellationTokenSource
    {
        public AsyncTimer (Func<Task> callback, int millisecondsDueTime, int millisecondsPeriod)
        {
            Task.Run(async () =>
            {
                await Task.Delay(millisecondsDueTime, Token);
                while (!IsCancellationRequested)
                {
                    await callback();
                    if (!IsCancellationRequested)
                        await Task.Delay(millisecondsPeriod, Token).ConfigureAwait(false);
                }
            });
        }
    
        protected override void Dispose(bool disposing)
        {
            if (disposing)
                Cancel();
    
            base.Dispose(disposing);
        }
    }
    
    {
      ...
      var timer = new AsyncTimer(OnTimer, 0, 1000);
    }
    
    private async Task OnTimer()
    {
       // Do something
       await MyMethodAsync();
    }