[英]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
集合? 如果是的話,請設定null
到ItemsSource
的的ListView
您設置之前List
到ItemsSource
中的ListView
。
我建議您使用ObservableCollection
。 它表示一個動態數據集合,當添加,刪除或刷新整個列表時會提供通知。
例如:
private ObservableCollection<Class4> Items;
public MainPage()
{
this.InitializeComponent();
Items = new ObservableCollection<Class4>();
verifyListView.ItemsSource = Items;
}
順便說一句,如果我們將ObservableCollection
設置為ListView
的ItemsSource
一次,則不再需要對其進行設置。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.