简体   繁体   English

在xamarin表单中通过计时器重复异步任务

[英]Repeat async task by timer in xamarin forms

i'm new to xamarin.forms development and i'm still having my first steps from the few tutorials that are found on the net. 我是xamarin.forms开发的新手,我还在网上找到的几个教程的第一步。 I have an async task that returns the time from date.jsontest.com and i have a timer that decrements a text in a label. 我有一个异步任务,从date.jsontest.com返回时间,我有一个计时器,减少标签中的文本。 i want to put the async task in the timer so that it repeats itself and displays the time on the label however im getting cannot convert async lamba to func 我想把异步任务放在计时器中,以便它重复自己并在标签上显示时间但是我得到的不能将async lamba转换为func

here's my code please help me, thanks 这是我的代码请帮助我,谢谢

static async Task<string> RequestTimeAsync()
    {
        using (var client = new HttpClient())
        {
            var jsonString = await client.GetStringAsync("http://date.jsontest.com/");
            var jsonObject = JObject.Parse(jsonString);
            return jsonObject["time"].Value<string>();
        }
    }


 protected override async void OnAppearing()
    {
        base.OnAppearing();

        timeLabel.Text = await RequestTimeAsync();

        Device.StartTimer(TimeSpan.FromSeconds(1), () => { // i want the taks to be put 
//here so that it gets repeated
            var number = float.Parse(button.Text) - 1;
            button.Text = number.ToString();
            return number > 0;
        });

    }

Reloading the Content Page in the timer would do the trick, so if anybody can please help me it would be appreciated 重新加载计时器中的内容页面可以解决问题,所以如果有人可以帮助我,我们将不胜感激

You just need to wrap the async method call in a Task.Run(), for example: 您只需要在Task.Run()中包装异步方法调用,例如:

Device.StartTimer(TimeSpan.FromSeconds(1), () =>
{
  Task.Run(async () =>
  {
    var time = await RequestTimeAsync();
    // do something with time...
  });
  return true;
});

Solved 解决了

as simple as that 就如此容易

 private async void ContinuousWebRequest()
    {
        while (_keepPolling)
        {
            timeLabel.Text = "call: "+counter+" "+ await RequestTimeAsync()+Environment.NewLine;
            // Update the UI (because of async/await magic, this is still in the UI thread!)
            if (_keepPolling)
            {
                await Task.Delay(TimeSpan.FromSeconds(5));
            }
        }
    }

    static async Task<string> RequestTimeAsync()
    {
        using (var client = new HttpClient())
        {
            var jsonString = await client.GetStringAsync("http://date.jsontest.com/");
            var jsonObject = JObject.Parse(jsonString);
            TimePage.counter++;
            return jsonObject["time"].Value<string>();
        }

    }

or you can try this: 或者你可以尝试这个:

static async Task<string> RequestTimeAsync()
{
    using (var client = new HttpClient())
    {
        var jsonString = await client.GetStringAsync("http://date.jsontest.com/");
        var jsonObject = JObject.Parse(jsonString);
        return jsonObject["time"].Value<string>();
    }
}


protected override async void OnAppearing()
{
    base.OnAppearing();

    var Text = await RequestTimeAsync();

    Device.StartTimer(TimeSpan.FromSeconds(1), () => { 
        // i want the taks to be put 
        //here so that it gets repeated
        var jsonDateTsk = RequestTimeAsync();
        jsonDateTsk.Wait();
        var jsonTime = jsonDateTsk.Result;
        var number = float.Parse(Text) - 1;
        var btnText = $"{number}";
        return number > 0;
    });
}

A simple clock demonstrates a one second timer action: 一个简单的时钟演示一秒计时器动作:

       Device.StartTimer(TimeSpan.FromSeconds(1), doitt);
        bool doitt()
        {
            label1.Text = DateTime.Now.ToString("h:mm:ss");
            return true;
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM