c# - 如何使用 Xamarin 显示通过 Web 服务获取的列表
问题描述
我可以使用数组来显示从 Web 服务获得的数据,并在列表视图中显示它们吗?
这是我的代码片段:
class WSResult2
{
public string url { get; set; }
public string equipment { get; set; }
public string serial { get; set; }
}
class WSResultList
{
public List<WSResult2> items { get; set; }
}
Page4.xaml.cs:
private async Task wsvarAsync(Result result)
{
WSClient client = new WSClient();
string wUrl = "--Here is the url of the web service--" + result.Text + "&option=2";
var resultws = await client.Get<WSResultList>(wUrl);
if (resultws != null)
{ //I want to know how to use an array here to get the complete list of each of the items captured by the web service.
lblEquipment.Text = resultws.items[0].equipment;
lblSerial.Text = resultws.items[0].serial;
lblurl.Text = resultws.items[0].url;
}
}
Page4.xaml:
<ContentPage.Content>
<StackLayout Padding="20">
<StackLayout HorizontalOptions="Center">
<Button x:Name="btnScan" Text="Read code" HorizontalOptions="FillAndExpand" VerticalOptions="EndAndExpand" BackgroundColor="LightBlue" TextColor="White" FontSize="Medium" CornerRadius="25" Clicked="btnScan_Clicked"/>
</StackLayout>
<Label x:Name="lblInfo" />
<Label Text="Equipment: " TextColor="Red"/>
<Label x:Name="lblEquipment" />
<Label Text="Serial: " TextColor="Red"/>
<Label x:Name="lblSerial" />
<!-- I want to know how to use a Binding to show all the data captured from the web service. -->
<Label IsVisible="False" Text="Report: " TextColor="Red"/>
<Label IsVisible="False" x:Name="lblurl" />
<Button x:Name="btnUrl" Text="See report" Clicked="OnButtonClicked" CornerRadius="25" HorizontalOptions="Center" TextColor="White" FontSize="Medium" BackgroundColor="Red"/>
</StackLayout>
</ContentPage.Content>
WSClient.cs 是反序列化 JSON 的客户端:
class WSClient
{
public async Task<T> Get<T>(string url)
{
HttpClient client = new HttpClient();
var response = await client.GetAsync(url);
var json = await response.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<T>(json);
}
public async Task<T> Post<T>(string pUrl, T pData)
{
var wJsonData = JsonConvert.SerializeObject(pData);
var wData = new StringContent(wJsonData, Encoding.UTF8, "application/json");
HttpClient client = new HttpClient();
var wResponse = await client.PostAsync(pUrl, wData);
var json = await wResponse.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<T>(json);
}
}
解决方案
Page4.xaml
<ListView x:Name="ResultList">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout>
<Label Text="{Binding url }"/>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate></ListView>
Page4.xaml.cs
private async Task wsvarAsync(Result result)
{
WSClient client = new WSClient();
var json = await client.GetStringAsync(wUrl);
var resultList = JsonConvert.DeserializeObject<List<WSResultList>>(json)
//to view each item on debug
for(var i = 0; i < resultList i++){
Console.WriteLine(resultList[i].equipment);
}
//populate the list view to visualize your data
ResulitList.ItemSource = resultList;
}
推荐阅读
- tensorflow - Tensorflow:TypeError:'NoneType'和'int'的实例之间不支持'>'
- html - 如何定位和 img 和 text 并将它们放在 html 中?
- python - 具有增加的起始索引的循环内的时间复杂度循环
- c# - 在C#的列中显示一行数据
- python - AssertionError:意外类型。预期的“列表”得到了“
' 使用 Pulumi Azure Native 创建 Azure Databricks 时出错 - go - 使用远程导入路径在vscode中调试golang代码时避免变量视图中的域名?
- php - SQL 更新语句抛出 SQLSTATE[42000]:语法错误或访问冲突:1064
- javascript - GatsbyJS:在 serviceworker 文件中导入 npm 包(gatsby-plugin-offline)
- sdk - Braintree 如何使用存储的信用卡付款
- c++ - 调整窗口大小导致我的 2D 照明拉伸