Refreshing or updating a list in a Windows Phone 8.1 app using C # and XAML
I need to update ListView
on upgrade ItemsSource
. I have declared myListView
as ListView
in a XAML file and assigned myList
as source to items in C # code with the following piece of code:
myListView.ItemsSource = myList;
Now, my question is, how can I update myListView
?
What do you mean by updating? If you want your UI to be updated, your myList must be of type ObservableCollection and your class must implement the INotifyPropertyChanged interface.
Check this message
https://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged%28v=vs.110%29.aspx
You need to use and ObservableCollection
public ObservableCollection<string> MyList { get; set; }
After setting the observable collection as your ItemsSource, every change to the collection will be automatically reflected in your list.
The great thing is that it only works with System.Collections.ObjectModel.ObservableCollection
and then set the collection to ListView.ItemsSource
.
try it
if (ListView.Items.Count > 0)
{
ListView.ItemsSource = null;
}
listItem.Clear();
Simply assigning new list view objects does not clear the data. You need to clear the ItemSource and also clear the list array used to bind the list view
I created a test app - krayknot to test this scenario and here is the code:
Include INotifyPropertyChanged
public sealed partial class PivotPage : Page, INotifyPropertyChanged
create event handler
public event PropertyChangedEventHandler PropertyChanged;
Create ObservableCollection
ObservableCollection<Feeds> oc = new ObservableCollection<Feeds>();
and here is the method,
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;
}