【发布时间】:2018-12-17 01:30:15
【问题描述】:
我有一个托管在服务器上的 WEB API,我有一个带有名称和描述的 Products 表。我已经检查了邮递员,这没关系,当我尝试通过 Visual Studio 在 xamarin 中实现该方法以带来按其名称记录并显示在列表视图中我不能。有人可以帮我写代码
private async void GetProductByName(string Name)
{
using (var client = new HttpClient())
{
txtTest.Text = "http://www.ProdutosAPITest6.hostname.com/api/products";
var URI = txtTest.Text + "/" + Name.ToString();
var response = await client.GetAsync(URI);
string products = await response.Content.ReadAsStringAsync();
var product = JsonConvert.DeserializeObject<Produto>(products);
listview.ItemsSource = products;
}
}
<ListView x:Name="ProductsList" ItemSelected="listaProducts_ItemSelected"
BackgroundColor="Aqua" SeparatorColor="Blue">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout Padding="10,10" Orientation="Horizontal">
<Label Text="{Binding Id}" HorizontalOptions="StartAndExpand"/>
<Label Text="{Binding Name}" TextColor="Blue" HorizontalOptions="Center"/>
<Label Text="{Binding Description}" HorizontalOptions="End"/>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
【问题讨论】:
-
出了什么问题?
-
我不知道列表视图不返回任何记录
-
您是否从服务器返回结果?请出示您的
Produto型号和ListView -
公共类产品 { public int Id { get;放; } 公共字符串名称 { 获取;放; } 公共字符串描述 { 获取;放; } }
-
listview.ItemsSource = 产品; products 是一个原始字符串,其中 product 是反序列化的 json。但是您只是反序列化单个 json 对象,而不是数组。发布的这段代码有很多很多错误。使用调试器单步执行代码并验证每一行是否符合您的预期。
标签: xamarin asp.net-web-api xamarin.forms asp.net-web-api2 asp.net-core-webapi