gpt4 book ai didi

c# - 如何在使用http请求读取json文件时在monotouch中显示进度条

转载 作者:行者123 更新时间:2023-11-30 14:13:35 25 4
gpt4 key购买 nike

我无法在我的 View 中显示进度条。真正令人高兴的是,当我单击该按钮时,它开始从服务器读取 json 文件,但在它尚未完成之前,您无法执行任何操作,甚至无法向 View 添加进度条。一旦加载,一切都会开始工作并显示进度条。

你知道我怎样才能实现这一点吗?一旦我点击按钮,它应该在完成后开始显示进度条,我将触发 dismiss 函数来关闭进度条。

这是我的脚本:

这是一个函数,在按钮触发时它开始调用函数并将进度条添加到 View 中

this.View.Add (loadingOverlay);
Getjsondata("http://polarisnet.my/polaristouchsales/Import/Products/product.json");
loadingOverlay.Hide ();

这里是Getjson函数

 public string Getjsondata(string URL)
{
HttpWebRequest request = null;
StreamReader responseReader = null;
string responseData = "";

try
{
request = (HttpWebRequest)HttpWebRequest.Create(URL);
responseReader = new StreamReader(request.GetResponse().GetResponseStream());
responseData = responseReader.ReadToEnd();
}
catch(Exception ex)
{
Console.WriteLine(ex.ToString());
}
finally
{
request.GetResponse().GetResponseStream().Close();
responseReader.Close();
responseReader = null;
}

return responseData;

}

最佳答案

您必须在单独的线程上执行 Getjsondata,如下所示:

this.View.Add (loadingOverlay);
ThreadPool.QueueUserWorkItem (() =>
{
Getjsondata("http://polarisnet.my/polaristouchsales/Import/Products/product.json");
BeginInvokeOnMainThread (() =>
{
loadingOverlay.Hide ();
});
});

现在下载时不会阻塞主线程,您可以在下载过程中继续更新您的 UI。

您还应该阅读有关 threading in MonoTouch 的文档- 它稍微解释了您可以做什么和不能做什么。

关于c# - 如何在使用http请求读取json文件时在monotouch中显示进度条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13853045/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com