每当我更新uwp中的对象列表时,如何更新我的listview项目

时间:2017-05-10 13:08:38

标签: c# uwp

将数据发送到Web服务的方法

async void getData(string center)
{
    string url = "http://10.91.91.50:7500/NNRAService/webresources/NNRA/getMedicalFilter?centerName="+center;
    HttpClient client = new HttpClient();
    //string response = await client.GetStringAsync(url);
    //var data = JsonConvert.DeserializeObject(response);
    //tbOutputText.Text = data.centerName.ToString();
    //tbOutputText.Text= data.ToString();

    HttpResponseMessage response = await client.GetAsync(new Uri(url));
    var jsonString = await response.Content.ReadAsStringAsync();
    JsonArray root = JsonValue.Parse(jsonString).GetArray();
    verifyList.Clear();
    for (uint i = 0; i < root.Count; i++)
    {
        string CenterName = root.GetObjectAt(i).GetNamedString("centerName");
        string Address = root.GetObjectAt(i).GetNamedString("address");
        string status = root.GetObjectAt(i).GetNamedNumber("isActive").ToString();
        if(status=="1")
        {
            active = "SAFE";
        }

        var verified = new Class4
        {
            centerName = CenterName,
            address = Address,
            isActive = active,

        };
        verifyList.Add(verified);
    };

    verifyListView.ItemsSource = verifyList;
}

当我调用该方法时,我希望它以这样的方式工作:当我更改文本框中的文本并单击按钮时,它应该在列表视图中为我生成另一个列表

private void btnVerify_Click(object sender, RoutedEventArgs e)
{          
    vtext = vCenter.Text;
    if(vtext != null)
    {
        getData(vtext);
    }        
}

1 个答案:

答案 0 :(得分:1)

您是否使用List保存Class4收藏集?如果是,请在将null设置为ItemsSource的{​​{1}}之前将ListView设置为List {/ 1}}。

我建议您使用ItemsSource。它表示动态数据集合,在添加,删除项目或刷新整个列表时提供通知。

例如:

ListView

顺便说一句,如果我们将ObservableCollection设置为   private ObservableCollection<Class4> Items; public MainPage() { this.InitializeComponent(); Items = new ObservableCollection<Class4>(); verifyListView.ItemsSource = Items; } 的{​​{1}}一次,我们不再需要设置它。

相关问题