使用json序列化绑定xaml的问题



我在使用json序列化的带有REST API的xamarin forms.xaml中显示数据时遇到问题。

控制台。WriteLine("测试产品:"+ProductList.Count(;确实返回了productlist(55(,但它没有查看。

我也有这个错误

[0:]无法将当前JSON对象(例如{"name"value"}(反序列化为类型"System"。集合。ObjectModel。ObservableCollection"1[xxx_xxxxx.Models.Category]",因为该类型需要JSON数组(例如[1,2,3](才能正确反序列化。要修复此错误,请将JSON更改为JSON数组(例如[1,2,3](,或者更改反序列化类型,使其成为正常类型。NET类型(例如,不是像integer这样的基元类型,也不是像数组或List这样的集合类型(,可以从JSON对象反序列化。JsonObjectAttribute也可以添加到类型中,以强制它从JSON对象反序列化。路径"消息",第1行,位置11。:\t错误{0}

我搞不清楚问题出在哪里。

RESTAPIService.cs

public RestAPIService()
{
client = new HttpClient { BaseAddress = new Uri("https://xxx.xxxxxxx.xx/xx/") };
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
}
public async Task<ObservableCollection<Products>> GetProducts()
{
products = new ObservableCollection<Products>();
var token = SecureStorage.GetAsync("AuthKey");
client.DefaultRequestHeaders.Add("www-authentication", await token);

try
{
HttpResponseMessage response = await client.GetAsync("products");
if (response.IsSuccessStatusCode)
{
var content = await response.Content.ReadAsStringAsync();
products = JsonConvert.DeserializeObject<ObservableCollection<Products>>(content);
Console.WriteLine("response products: " + products.Count);
}
}
catch (Exception ex)
{
Debug.WriteLine(@"tERROR {0}", ex.Message);
}
return products;
}

Products.cs我的模态类

public class ImageFront
{
public string extension { get; set; }
public string name { get; set; }
public string contents { get; set; }
public int size { get; set; }
public string date { get; set; }
public object thumb { get; set; }
public int? width { get; set; }
public int? height { get; set; }
}
public class ImageThumb
{
public string extension { get; set; }
public string name { get; set; }
public string contents { get; set; }
public int size { get; set; }
public string date { get; set; }
public object thumb { get; set; }
public int? width { get; set; }
public int? height { get; set; }
}
public class File
{
public string extension { get; set; }
public string name { get; set; }
public string contents { get; set; }
public int size { get; set; }
public string date { get; set; }
}
public class Product
{
public string id { get; set; }
public string number { get; set; }
public string name { get; set; }
public string indication { get; set; }
public string weight { get; set; }
public string amount { get; set; }
public string kind { get; set; }
public string usage_days { get; set; }
public string usage_dosage { get; set; }
public string usage_how { get; set; }
public string dosage { get; set; }
public string allergie { get; set; }
public string nutrition { get; set; }
public string preserve { get; set; }
public string shelf_distribution { get; set; }
public string shelf_prod_nr { get; set; }
public string shelf_life { get; set; }
public string charge { get; set; }
public string shelf_description { get; set; }
public string webtext { get; set; }
public string daily_dosage { get; set; }
public object ingredients { get; set; }
public object composition { get; set; }
public string factor { get; set; }
public string risk_analysis { get; set; }
public string conclusion { get; set; }
public string color { get; set; }
public string label { get; set; }
public string purchase_price { get; set; }
public double price { get; set; }
public double sales_price { get; set; }
public string tax_rate { get; set; }
public string min_stock { get; set; }
public string category { get; set; }
public string fabrikant_id { get; set; }
public bool archive { get; set; }
public bool active { get; set; }
public string price_original { get; set; }
public string sales_price_original { get; set; }
public int current_stock { get; set; }
public bool stock_warning { get; set; }
public ImageFront image_front { get; set; }
public ImageThumb image_thumb { get; set; }
public List<File> File { get; set; }
}
public class Products
{
public Product Product { get; set; }
}

ProductViewModel.cs

public class ProductViewModel : BaseViewModel
{
private ObservableCollection<Products> products;
public ProductViewModel()
{
ProductList = new ObservableCollection<Products>();
GetProducts();
}
public ObservableCollection<Products> ProductList
{
get { return products; }
set
{
products = value;
OnPropertyChanged();
}
}
public async void GetProducts()
{
ProductList.Clear();
ProductList = await App.RestAPIManager.GetProductsAsync();

Console.WriteLine("test products: " + ProductList.Count);
}
}

产品页面.xaml

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="xxx_xxxxx.Views.ProductPage"
xmlns:vm="clr-namespace:xxx_xxxxx.ViewModels" xmlns:model="clr-namespace:xxx_xxxxxx.xxxxxx">

<ContentPage.BindingContext>
<vm:ProductViewModel/>
</ContentPage.BindingContext>
<StackLayout>        
<CollectionView x:Name="productList"
ItemsSource="{Binding ProductList}"
VerticalScrollBarVisibility="Never" 
SelectionMode="Single"
BackgroundColor="LightGray">
<CollectionView.ItemTemplate>
<DataTemplate>
<StackLayout Padding="10">
<Label Text="{Binding Product.name}" 
FontSize="Small"
TextColor="Blue"/>
<Label Text="{Binding Product.price}" 
FontSize="Small" />
<Button Text="hoi">

</Button>
</StackLayout>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
</StackLayout>
</ContentPage>

绑定时尝试删除Product的前缀。

<CollectionView x:Name="productList"
ItemsSource="{Binding ProductList}"
VerticalScrollBarVisibility="Never" 
SelectionMode="Single"
BackgroundColor="LightGray">
<CollectionView.ItemTemplate>
<DataTemplate>
<StackLayout Padding="10">
<Label Text="{Binding name}" 
FontSize="Small"
TextColor="Blue"/>
<Label Text="{Binding price}" 
FontSize="Small" />
<Button Text="hoi">

</Button>
</StackLayout>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>

注意:我们应该这样编码:

<Label Text="{Binding name}" 
FontSize="Small"
TextColor="Blue"/>

而不是以下代码:

<Label Text="{Binding Product.name}" 
FontSize="Small"
TextColor="Blue"/>

最新更新