gpt4 book ai didi

c# - 下载数据时在 MonoTouch 中显示微调器

转载 作者:行者123 更新时间:2023-11-28 19:10:35 25 4
gpt4 key购买 nike

因此,在我的应用程序中,我有一个按钮可以与从互联网下载一些数据并对其进行过滤的库进行对话。当应用程序执行此操作时,屏幕会卡住,并且在用户看来应用程序崩溃了。但事实并非如此,因为它正在下载数据。

这是我的代码:

GetDetailsBtn.TouchUpInside += (sender, e) => {

var defaults = NSUserDefaults.StandardUserDefaults;

if (RefNr.Text != string.Empty && RefNr.Text != null) {

FilteredDataRef = _FetchingData.getTrackTraceData (defaults.StringForKey ("SecurityToken"), RefNr.Text);

if (FilteredDataRef == null) {
UIAlertView InvalidAlert = new UIAlertView ("Reference number invalid", "The reference number that you have entered is not linked to the current security code. You can change your security code in the settings.", null, "OK", null);
InvalidAlert.Show ();

} else {
FilteredDataReceived = _FetchingData.FilteringOnReceived (FilteredDataRef);

FilteredDataPlanned = _FetchingData.FilteringOnPlanned (FilteredDataRef);

FilteredDataLoadingETA = _FetchingData.FilteringOnLoadingETA (FilteredDataRef);

FilteredDataLoadingFinal = _FetchingData.FilteringOnLoadingFinal (FilteredDataRef);

FilteredDataUnloadingETA = _FetchingData.FilteringOnUnloadingETA (FilteredDataRef);

FilteredDataUnloadingFinal = _FetchingData.FilteringOnUnloadingFinal (FilteredDataRef);

this.PerformSegue (MoveToTrackTraceDetailsSegue, this);

//foreach (string s in FilteredDataPlanned)
// Console.WriteLine (s);

}

} else {
UIAlertView InvalidAlert = new UIAlertView ("Reference number cannot be empty", "You did not provide a reference number. We need your reference number to trace identify the shipment you would like to trace.", null, "OK", null);
InvalidAlert.Show ();
}
};

数据下载:

public IEnumerable<string> getTrackTraceData (string securityCode, string referenceNumber)
{
WebRequest request = WebRequest.Create ("http://plex.janssen1877.com/app/life/" + securityCode);

HttpWebResponse response = (HttpWebResponse)request.GetResponse ();

Stream dataStream = response.GetResponseStream ();

StreamReader reader = new StreamReader (dataStream);

string FetchedData = reader.ReadToEnd ();

reader.Close ();

dataStream.Close ();

response.Close ();

var FetchingDataItems = FetchedData.Split (new char[] { '\n' });

if (FetchingDataItems != null) {
var filteredResult = FetchingDataItems.Where (x => x.Contains (referenceNumber));

return filteredResult;
} else {
return null;
}
}

现在我想使用一个名为 BTProgressHUD 的组件。这只是一个花哨的微调器。我想如果我将 BTProgressHUD.show(); 放在按钮操作的顶部并将 BTProgressHUD.Dismiss(); 放在按钮上,它会在加载时显示加载完成后启动并关闭。

事实并非如此。它在新的 View Controller 中显示得非常快,并在一秒钟内再次消失。我做错了什么?

例如编辑:

public IEnumerable<string> getTrackTraceData (string securityCode, string referenceNumber)
{


string url = string.Format ("http://plex.janssen1877.com/app/life/" + securityCode);

HttpWebRequest HttpRequest = (HttpWebRequest)WebRequest.Create (url);

string FetchedData = new StreamReader (HttpRequest.GetResponse ().GetResponseStream ()).ReadToEnd ();


var FetchingDataItems = FetchedData.Split (new char[] { '\n' });

if (FetchingDataItems != null) {
var filteredResult = FetchingDataItems.Where (x => x.Contains (referenceNumber));

return filteredResult;
} else {
return null;
}


}

最佳答案

弗洛里安

根据 .NET 文档和 HttpWebRequest GetResponse. How to wait for that response?您需要以异步方式执行下载(可能还有解析)。

您的实际应用程序的行为是正确的。当您在主线程中执行同步请求时,您会卡住应用程序,因此不会更新 UI 元素。主线程以串行方式处理执行。

为避免这种情况,您有两种不同的解决方案。一方面,您需要转向异步请求。另一方面,您可以使用同步请求创建后台线程(不同的执行路径)。我更喜欢前者。因此,例如,在启动异步请求后,显示指示器。完成后(在回调中),关闭指示器并执行 segue。

例如,您可以按照以下关于如何实现此目的的讨论进行操作:How to use HttpWebRequest (.NET) asynchronously? .

要了解主线程(和运行循环)的工作原理,我建议阅读 The pogo stick of NSRunLoop .

希望对您有所帮助。

编辑

您应该使用如下模式(来源 How to use HttpWebRequest (.NET) asynchronously? ):

HttpWebRequest webRequest;

void StartWebRequest()
{
webRequest.BeginGetResponse(new AsyncCallback(FinishWebRequest), null);
}

void FinishWebRequest(IAsyncResult result)
{
webRequest.EndGetResponse(result);
}

关于c# - 下载数据时在 MonoTouch 中显示微调器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16046144/

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