如何检查Xamarin.forms C#中是否有长时间的数据下载>> [

问题描述 投票:0回答:1
我正在完成一项任务,在下载数据时,我们会在用户手机上显示一个ActivityIndi​​cator。现在,当后台下载正在运行时,当前的ActivityIndi​​cator显示一个标签“ Loading ...”。但是,如果下载时间超过20秒,我需要将标签从“正在加载...”更新为“仍在下载...”。

我试图弄清楚如何使用C#中的计时器功能来检查我的下载是否已经运行了20秒以上。根据我的理解,OnTimedEvent()仅在经过设置时间后才会触发,但是我需要并行执行下载过程。以下是我要完成的工作。

SetTimer(20000, "Still Downloading...") // Here while the below api call is running, if it takes more than 20 seconds to complete then fire up the event to update the loading label. var response = obj.GetFileData(JsonConvert.SerializeObject(inputJson));

下面是我从here读取的计时器功能

public static void SetTimer(int timerTime, string eventMessage) { if (timerTime > 0) { _timer = new Timer(timerTime); _timer.Elapsed += (sender, e) => { OnTimedEvent(eventMessage); }; _timer.AutoReset = false; _timer.Enabled = true; } } public static void OnTimedEvent(string eventMessage) { mylabel.text = eventMessage; }

我不确定在这里使用计时器类的方法是否正确。我遇到了许多有关计时器类的帖子,但他们都在谈论在计时器经过时触发事件,但没有谈论与我的api调用并行运行计时器。 

任何帮助将不胜感激。

我正在完成一项任务,在下载数据时,我们会在用户手机上显示一个ActivityIndi​​cator。现在,当后台...

c# xamarin.forms timer download-manager
1个回答
0
投票
尝试像这样在主线程中运行:

public void OnTimedEvent(string eventMessage) { Device.BeginInvokeOnMainThread(() => { mylabel.Text = eventMessage; }); }

© www.soinside.com 2019 - 2024. All rights reserved.