using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.ComponentModel; using System.IO; using System.Net; using System.Net.Browser; using System.Runtime.Serialization.Json; using System.Windows.Media.Imaging; using Juick.Api; using RestSharp; namespace Juick.ViewModels { public class ThreadViewModel : INotifyPropertyChanged { public ThreadViewModel() { this.Items = new ObservableCollection(); } /// /// A collection for MessageViewModel objects. /// public ObservableCollection Items { get; private set; } public bool IsDataLoaded { get; private set; } public MessageViewModel Root { get; set; } /// /// Creates and adds a few MessageViewModel objects into the MyFeed collection. /// public void LoadData() { var request = new RestRequest("/thread?mid={mid}" + "&rnd=" + Environment.TickCount); request.AddUrlSegment("mid", string.Format("{0}",Root.MID)); App.Client.Authenticator = new HttpBasicAuthenticator(App.Account.Credentials.UserName, App.Account.Credentials.Password); App.Client.ExecuteAsync>(request, response => { var messages = response.Data; Items.Clear(); messages.ForEach(post => { var item = new MessageViewModel(post); Items.Add(item); var imageRequest = new RestRequest( string.Format("/as/{0}.png", post.User.Uid)); App.AvatarClient.ExecuteAsync( imageRequest, restResponse => { item.UserAvatar = new BitmapImage (); item.UserAvatar.SetSource(new MemoryStream(restResponse.RawBytes)); item. NotifyPropertyChanged ("UserAvatar"); }); }); IsDataLoaded = true; NotifyPropertyChanged("Items"); }); } public event PropertyChangedEventHandler PropertyChanged; private void NotifyPropertyChanged(String propertyName) { PropertyChangedEventHandler handler = PropertyChanged; if (null != handler) { handler(this, new PropertyChangedEventArgs(propertyName)); } } } }