使用c#和XAML更新或刷新Windows Phone 8.1应用程序中的Listview
本文关键字:应用程序 Listview Phone Windows XAML 更新 刷新 使用 | 更新日期: 2023-09-27 17:54:54
当ItemsSource
更新时,我需要刷新ListView
。我已经将myListView
声明为XAML文件中的ListView
,并将myList
指定为c#代码中的itemssource,代码片段如下:
myListView.ItemsSource = myList;
现在,我的问题是,如何刷新myListView
?
你说的刷新是什么意思?如果你想要你的UI被刷新,你的myList必须是ObservableCollection类型,你的类必须实现INotifyPropertyChanged接口。
查看这篇文章
https://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged%28v=vs.110%29.aspx你需要使用and ObservableCollection
public ObservableCollection<string> MyList { get; set; }
将可观察对象集合设置为ItemsSource后,对该集合的任何更改都会自动反映在listview中。
很高兴看到它只能通过使用System.Collections.ObjectModel.ObservableCollection
来工作,然后在ListView.ItemsSource
中设置集合。
试试这个
if (ListView.Items.Count > 0)
{
ListView.ItemsSource = null;
}
listItem.Clear();
仅仅赋值新的列表视图对象不会清除数据。您需要清除ItemSource,还需要清除用于绑定列表视图的列表数组
我创建了一个测试应用程序- krayknot来测试这个场景,下面是代码:
包括INotifyPropertyChanged
public sealed partial class PivotPage : Page, INotifyPropertyChanged
创建事件
public event PropertyChangedEventHandler PropertyChanged;
创建ObservableCollection ObservableCollection<Feeds> oc = new ObservableCollection<Feeds>();
这里是方法,
private async void BindFeeds(string URL)
{
progressRing.IsActive = true;
var uri = new Uri(URL);
var httpClient = new HttpClient();
// Always catch network exceptions for async methods
httpClient.DefaultRequestHeaders.Add("Cache-Control", "no-cache");
var result = await httpClient.GetAsync(uri);
var xmlStream = await result.Content.ReadAsStreamAsync();
XDocument loadedData = XDocument.Load(xmlStream);
foreach (var data in loadedData.Descendants("Table"))
{
try
{
oc.Add(new Feeds
{
BlogId = data.Element("Blogs_CID").Value.ToString(),
Description = data.Element("Blogs_BriefDescription").Value.ToString(),
IconImage = videoIcon,
Heading = data.Element("Blogs_Heading").Value.ToString().Trim(),
MainImage = feedImageThumbnail, //data.Element("Blogs_SquareImagePath").Value.ToString(),
TimeStamp = data.Element("TimeElapsed").Value.ToString(),
SourceURL = data.Element("Blogs_SourceURL").Value.ToString()
});
}
catch (Exception)
{
//throw;
}
}
httpClient.Dispose();
if (lstLatest.Items.Count == 0)
{
lstLatest.ItemsSource = oc;
}
else
{
NotifyPropertyChanged();
}
httpClient.Dispose();
progressRing.IsActive = false;
}