gpt4 book ai didi

c# - 线程启动<线程池> : When i get data from api

转载 作者:太空宇宙 更新时间:2023-11-03 15:36:27 25 4
gpt4 key购买 nike

我想从 api 获取数据并将其放入组 ListView ,但应用程序不显示错误也不显示数据。对不起我的英语不好

这是我下载数​​据的函数:

public async Task<List<GroupInvoiceInList>> test_connection()
{
var listItems = new List<GroupInvoiceInList> ();
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Add ("token","40df006e4c4314aefd892e335a743338c8d3331e");

var response = await client.GetAsync(SuperVAR.URL_CUSTOMER_GET_LISTINVOICE+"?limit=15");
if (response.IsSuccessStatusCode)
{
JObject res = JObject.Parse (response.Content.ReadAsStringAsync ().Result);
string message = res ["message"].ToString ();
if (message.Equals ("Success")) {
JArray arrayData = (JArray)res["data"]["invoiceList"];
GroupInvoiceInList g1 = new GroupInvoiceInList (1437880499);
for (int i = 0; i < arrayData.Count; i++) {
var item = (JObject)arrayData [i];

InvoiceInList hihi = new InvoiceInList (Int32.Parse(item["invoiceStatus"].ToString()));
hihi.invoiceId = Int32.Parse(item["invoiceId"].ToString());
hihi.invoiceProductName = item["productName"].ToString();
hihi.contactPhone = item["receiverPhone"].ToString();
hihi.invoiceReceiverName = item["receiverName"].ToString();
hihi.invoiceReceiverAddress = item["receiverFullAddress"].ToString();
hihi.invoiceCodFee = Double.Parse(item["codFee"].ToString());
hihi.invoiceSumFee = Double.Parse(item["sumFee"].ToString());
g1.Add (hihi);

Debug.WriteLine(hihi.invoiceId+"");
}
listItems.Add (g1);

} else {
//DisplayAlert ("", message, "OK");
}
}
}
return listItems;
}

以及在布局中显示的代码

var listItems = test_connection ().Result;
listView = new ListView
{
SeparatorVisibility = SeparatorVisibility.None,
HasUnevenRows = true,
ItemTemplate = new DataTemplate(typeof(InvoiceInListCell)),
IsGroupingEnabled = true,
GroupHeaderTemplate = new DataTemplate(typeof(GroupInvoiceInListCell)),
ItemsSource = listItems
};
this.Content = new StackLayout
{
Children = {listView },
BackgroundColor = Color.White,
};

数据模型:GroupInvoiceInList

public class GroupInvoiceInList : ObservableCollection<InvoiceInList>
{
public int dayOfMonth { get; set;}
public string dayOfWeek { get; set; }
public string monthAndYear { get; set;}

public GroupInvoiceInList (long time)
{
DateTime timeA = SuperFUNC.FromUnixTime(time);
this.dayOfMonth = timeA.Day;
this.dayOfWeek = SuperFUNC.ConvertToDayOfWeek ((int)timeA.DayOfWeek);
this.monthAndYear = SuperFUNC.ConvertToMonthOfYear ((int)timeA.Month) + " " + timeA.Year;
}
}

发票 list

public class InvoiceInList
{
public int invoiceId{ set; get;}
public string invoiceStatus{ set; get;}
public string contactPhone{ set; get;}
public string invoiceProductName { set; get;}
public string invoiceReceiverName { set; get;}
public string invoiceReceiverAddress { set; get;}
public double invoiceCodFee{ set; get;}
public double invoiceSumFee{ set; get;}
public long invoiceCreateDate{ set; get;}
public Color backGroundTop { set; get;}

public InvoiceInList (int invoiceStatus)
{
this.invoiceStatus = SuperFUNC.ConvertStatusToString (invoiceStatus);
this.backGroundTop = SuperFUNC.ConvertStatusToColor (invoiceStatus);
}
}

应用输出:

 Thread started: <Thread Pool> #5
Thread started: <Thread Pool> #6

我是c#和xamarin的新手,感谢阅读

最佳答案

我认为在与 UI 交互相同的线程上运行 json 连接可能是一个问题,因此无需等待, ListView 会在返回数据之前更新。

我在我的代码中使用单一责任并使用事件处理程序来检测数据集何时更改以重新绑定(bind) ListView 上的数据。我的示例是 XMl,但概念是相同的。

Listview 数据绑定(bind)引用:

http://developer.xamarin.com/guides/cross-platform/xamarin-forms/introduction-to-xamarin-forms/#Customizing_the_Appearance_of_a_Cell

在页面出现事件时调用

var response = await DataHelper.GetData();

我的数据助手类中的 HTTP 请求

 public static async Task<string> GetData()
{
var result = true;
var url = "API Call URL";

var content = new Dictionary<string, string>();
content["needed parameter for API call"] = ID;
var response = await WebRequestHelper.MakeAsyncRequest(url, content);
if (response.IsSuccessStatusCode)
{
result = response.Content.ReadAsStringAsync().Result;
result = result.Replace("<xml>", "<ModelResultName>").Replace("</xml>", "</ModelResultName>");
}
else
{
result = false;
Debug.WriteLine("Failed to get data");
}
return result;

然后我有一个事件处理程序来检测更改并重新绑定(bind)我的 Listview 数据源

    public event EventHandler<ModelName> ResultUpdated;

private void ResultUpdated(object sender, GamesResult e)
{
Model "In your case InvoiceInList" itemSource = null;
ListView.ItemsSource = itemSource;
}

另请注意,您的 ListView ItemTemplate 应该绑定(bind)到模型上的数据模板绑定(bind),例如:

ListView.ItemTemplate = new DataTemplate(typeof(InvoiceInListCell))

Label invoiceProductName = new Label();
invoiceProductName .SetBinding(Label.TextProperty, "productName");

关于c# - 线程启动<线程池> : When i get data from api,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31742834/

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