You need to enable JavaScript to run this app. WPF绑定API返回Json时Country模型属性全为空ListView无显示内容
优惠活动
大模型
产品
解决方案
定价
更多

WPF绑定API返回Json时Country模型属性全为空ListView无显示内容

问题根因

你遇到的空白展示问题和XAML数据绑定配置无关,核心原因是API返回的JSON字段名和你定义的Country模型属性名不匹配,导致Newtonsoft.Json反序列化时无法正确给属性赋值,最终得到的所有Country实例属性都为null。

解决方案

方法1(推荐):给模型属性添加JsonProperty映射

你使用的是Newtonsoft.Json进行反序列化,可以通过[JsonProperty]特性显式指定属性对应的JSON字段名,适配API返回的字段格式,修改后的Country模型示例如下:

public class Country
{
    [JsonProperty("Active Cases_text")] // 可根据你实际拿到的JSON字段名调整对应值
    public string ActiveCasesText { get; set; }
    
    [JsonProperty("Country_text")]
    public string CountryText { get; set; }
    
    [JsonProperty("Last Update")]
    public string LastUpdate { get; set; }
    
    [JsonProperty("New Cases_text")]
    public string NewCasesText { get; set; }
    
    [JsonProperty("New Deaths_text")]
    public string NewDeathsText { get; set; }
    
    [JsonProperty("Total Cases_text")]
    public string TotalCasesText { get; set; }
    
    [JsonProperty("Total Deaths_text")]
    public string TotalDeathsText { get; set; }
    
    [JsonProperty("Total Recovered_text")]
    public string TotalRecoveredText { get; set; }
}

方法2:直接修改模型属性名和JSON字段完全一致

如果不想加特性,也可以把Country类的属性名直接改成和API返回的JSON字段名完全一致,但如果JSON字段包含空格、特殊字符时该方法不适用。

补充检查项

确认你在窗口加载逻辑中调用了CasesVM的MakeQuery()方法,否则请求不会触发,集合不会加载数据。


内容的提问来源于stack exchange,提问作者ArniePie

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.07 22:18:00