What you need to do is create a persistent, bindable collection object somewhere in your view and assign that to ItemsSource once. As new items are received, just add them to the collection You'll want to either choose an existing collection type that supports notifications when the collection is modified or implement one yourself. In Silverlight, I think that your best (only?) bet is the System.Collections.ObjectModel.
ObservableCollection The other option is to implement System.Collections.Specialized. INotifyCollectionChanged in a custom class that inherits from a different type of Collection As for sorting, if your arriving data will always be later than your existing data, then you can just sort the new items prior to adding them to the collection (you may want to insert them in the front of the collection if you want to display them with the newest on the top) However, if the entire collection needs to be sorted each time new records are added, then you will need to implement System. IComparable on your item class.
In this case, I would recommend the following approach: Create a new collection class based on System.Collection.Generic. List (this contains a native Sort() method) Implement INotifyCollectionChanged in this class and raise its CollectionChanged event with the NotifyCollectionChangedAction. Reset action after your records are added and sorted Implement IComparable on your item class to get the items sorted according to your rules Update with implementation of INotifyCollectionChanged Most of the suggestion is pretty straightforward, but the implementation of the INotifyCollectionChanged is a little tricky, so I am including it here: NonSerialized()> _ Private m_ListChangedEvent As NotifyCollectionChangedEventHandler ''' ''' This event is raised whenever the list is changed and implements the IBindingList ListChanged event ''' ''' ''' ''' Public Custom Event ListChanged As NotifyCollectionChangedEventHandler Implements INotifyCollectionChanged.
CollectionChanged _ AddHandler(ByVal value As NotifyCollectionChangedEventHandler) m_ListChangedEvent = DirectCast(Delegate. Combine(m_ListChangedEvent, value), NotifyCollectionChangedEventHandler) End AddHandler _ RemoveHandler(ByVal value As NotifyCollectionChangedEventHandler) m_ListChangedEvent = DirectCast(Delegate. Remove(m_ListChangedEvent, value), NotifyCollectionChangedEventHandler) End RemoveHandler RaiseEvent(ByVal sender As Object, ByVal e As NotifyCollectionChangedEventArgs) If m_ListChangedEvent IsNot Nothing Then m_ListChangedEvent.
Invoke(sender, e) End If End RaiseEvent End Event To raise this event so that consumers are aware of changes to the list: Call RaiseListChangedEvent(New NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction. Reset)).
What you need to do is create a persistent, bindable collection object somewhere in your view and assign that to ItemsSource once. As new items are received, just add them to the collection. You'll want to either choose an existing collection type that supports notifications when the collection is modified or implement one yourself.In Silverlight, I think that your best (only?
) bet is the System.Collections.ObjectModel. ObservableCollection. The other option is to implement System.Collections.Specialized.
INotifyCollectionChanged in a custom class that inherits from a different type of Collection. As for sorting, if your arriving data will always be later than your existing data, then you can just sort the new items prior to adding them to the collection (you may want to insert them in the front of the collection if you want to display them with the newest on the top). However, if the entire collection needs to be sorted each time new records are added, then you will need to implement System.
IComparable on your item class. In this case, I would recommend the following approach: Create a new collection class based on System.Collection.Generic. List (this contains a native Sort() method).
Implement INotifyCollectionChanged in this class and raise its CollectionChanged event with the NotifyCollectionChangedAction. Reset action after your records are added and sorted. Implement IComparable on your item class to get the items sorted according to your rules.
Update with implementation of INotifyCollectionChanged Most of the suggestion is pretty straightforward, but the implementation of the INotifyCollectionChanged is a little tricky, so I am including it here: _ Private m_ListChangedEvent As NotifyCollectionChangedEventHandler ''' ''' This event is raised whenever the list is changed and implements the IBindingList ListChanged event ''' ''' ''' ''' Public Custom Event ListChanged As NotifyCollectionChangedEventHandler Implements INotifyCollectionChanged. CollectionChanged _ AddHandler(ByVal value As NotifyCollectionChangedEventHandler) m_ListChangedEvent = DirectCast(Delegate. Combine(m_ListChangedEvent, value), NotifyCollectionChangedEventHandler) End AddHandler _ RemoveHandler(ByVal value As NotifyCollectionChangedEventHandler) m_ListChangedEvent = DirectCast(Delegate.
Remove(m_ListChangedEvent, value), NotifyCollectionChangedEventHandler) End RemoveHandler RaiseEvent(ByVal sender As Object, ByVal e As NotifyCollectionChangedEventArgs) If m_ListChangedEvent IsNot Nothing Then m_ListChangedEvent. Invoke(sender, e) End If End RaiseEvent End Event To raise this event so that consumers are aware of changes to the list: Call RaiseListChangedEvent(New NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction. Reset)).
Sounds difficult (but fun)! I'm assuming that looking through MSDN will give me what I need to know to do such a thing? If not, do you have any good tutorial / links for general Notification implementations and so on?
– Josh Nov 13 at 2:08 1 The INotifyCollectionChanged implementation is the most challenging, but I included my solution in the answer. The rest should be easily discoverable (sorry, I don't have any specific links since we implemented this last year sometime). – competent_tech Nov 13 at 2:16 No problem, I really appreciate all you've given me.
I'm a professional developer, but have only been doing MS / C# coding for about 5 days now! – Josh Nov 13 at 2:18 1 Sorry, forgot this was a C# question; the code I added was VB, but there should be translations out there. The one important note is that if the item class is serialized, you don't want to serialize the event with it since it is technically a serializable member, which I have never really understood.
– competent_tech Nov 13 at 2:21.
A simply solution would be to use an ObservableCollection as your ItemsSource: TwitterListBox. ItemsSource = new ObservableCollection(). Whenever you receive more items to add - simply do: var itemsSource = (ObservableCollection)TwitterListBox.
ItemsSource; foreach(var twitterItem in newTweets) { itemsSource. Add(twitterItem); } If you want these sorted - you need to do itemsSource. Insert(twitterItem, i) after you figure out where to insert the new item.
There are probably a few ways to do that, but assuming you parse your created_at like this: CreatedAt = DateTime. ParseExact(createdAt, "ddd MMM dd HH:mm:ss zzz yyyy", CultureInfo. InvariantCulture) This is roughly how you could do it: int I = 0; // insert index int j = 0; // new items index while (j = newTweetsj.
CreatedAt) { i++; } itemsSource. Insert(i, newTweetsj); j++; } Or a fancier solution: public partial class MainPage : PhoneApplicationPage { // Constructor public MainPage() { InitializeComponent(); var itemsSource = new ObservableCollection(); var initialTweets = new { new TwitterItem {CreatedAt = DateTime.Now. AddMinutes(-3)}, new TwitterItem {CreatedAt = DateTime.Now.
AddMinutes(-2)}, new TwitterItem {CreatedAt = DateTime.Now. AddMinutes(-1)} }; itemsSource. Merge(initialTweets.
OrderByDescending(ti => ti. CreatedAt)); var newTweets = new List(); newTweets. Add(new TwitterItem {CreatedAt = DateTime.Now.
AddMinutes(-3.5)}); newTweets. Add(new TwitterItem {CreatedAt = DateTime.Now. AddMinutes(-2.5)}); newTweets.
Add(new TwitterItem {CreatedAt = DateTime.Now. AddMinutes(-1.5)}); newTweets. Add(new TwitterItem {CreatedAt = DateTime.Now.
AddMinutes(-0.5)}); itemsSource. Merge(newTweets. OrderByDescending(ti => ti.
CreatedAt)); foreach (var twitterItem in itemsSource) { Debug. WriteLine(twitterItem.CreatedAt.ToString()); } } } public class TwitterItem { public DateTime CreatedAt; } public static class ObservableTwitterItemsExtensions { public static void Merge( this ObservableCollection target, IEnumerable source) { int I = 0; // insert index foreach (var newTwitterItem in source) { while (i = newTwitterItem. CreatedAt) { i++; } target.
Insert(i, newTwitterItem); } } }.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.