簡體   English   中英

每當我更新uwp中的對象列表時,如何更新listview項目

[英]how can i update my listview items whenever i update my list of object in uwp

向網絡服務發送數據的方法

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);
    }        
}

您是否使用List保存Class4集合? 如果是的話,請設定nullItemsSource的的ListView您設置之前ListItemsSource中的ListView

我建議您使用ObservableCollection 它表示一個動態數據集合,當添加,刪除或刷新整個列表時會提供通知。

例如:

 private ObservableCollection<Class4> Items;
 public MainPage()
 {
     this.InitializeComponent();
     Items = new ObservableCollection<Class4>();
     verifyListView.ItemsSource = Items;
 }

順便說一句,如果我們將ObservableCollection設置為ListViewItemsSource一次,則不再需要對其進行設置。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM