/Samples/Controls/Marquee/WPF/Marquee-WPF/NewsViewModel.cs
C# | 122 lines | 91 code | 19 blank | 12 comment | 2 complexity | aab81164e88d0b01b8cf0115e79ce251 MD5 | raw file
- using System;
- using System.Collections.ObjectModel;
- using System.ComponentModel;
- using System.Concurrency;
- using System.IO;
- using System.Linq;
- using System.Net;
- using System.Text;
- using System.Windows.Input;
- using System.Windows.Threading;
- using System.Xml.Linq;
-
- namespace Marquee_WPF
- {
- public class NewsViewModel : INotifyPropertyChanged
- {
- private IDisposable _observation;
- private Headline _expandedHeadline;
-
- public NewsViewModel()
- {
- Headlines = new ObservableCollection<Headline>();
-
- var webRequest = WebRequest.Create("http://feeds.bbci.co.uk/news/rss.xml");
-
- _observation = Observable.FromAsyncPattern<WebResponse>(webRequest.BeginGetResponse, webRequest.EndGetResponse)() // Execute the web request to get XML
- .Select(response => response.GetResponseStream())
- .Select(stream =>
- {
- using (var reader = new StreamReader(stream, Encoding.UTF8))
- return reader.ReadToEnd();
- })
- .Select(xml => XDocument.Parse(xml)) // Parse the XML
- .SelectMany(document => document.Descendants("channel").Descendants("item").ToObservable()) // Extract the item elements
- .Select(xelem => new Headline(this) // Convert from XML into our model class
- {
- Title = xelem.Descendants("title").First().Value,
- Summary = xelem.Descendants("description").First().Value,
- })
- .ObserveOn(new DispatcherScheduler(Dispatcher.CurrentDispatcher))
- .Subscribe(headline => Headlines.Add(headline)); // And add the object into the bound collection
- }
-
- /// <summary>
- /// The headline which currently has its summary displayed
- /// </summary>
- public Headline ExpandedHeadline
- {
- get
- {
- return _expandedHeadline;
- }
- set
- {
- _expandedHeadline = value;
- OnPropertyChanged("ExpandedHeadline");
- }
- }
-
-
- /// <summary>
- /// List of all headlines
- /// </summary>
- public ObservableCollection<Headline> Headlines { get; private set; }
-
- public event PropertyChangedEventHandler PropertyChanged;
-
- public void OnPropertyChanged(string propertyName)
- {
- PropertyChangedEventHandler handler = PropertyChanged;
- if (handler != null)
- handler(this, new PropertyChangedEventArgs(propertyName));
- }
- }
-
- /// <summary>
- /// An implementation of ICommand. It's "temporary" in the sense that it's not a very good implementation of ICommand, it's just about enough for the purpose of this example.
- /// </summary>
- public class TemporaryDelegateCommand : ICommand
- {
- private Action _action;
-
- public TemporaryDelegateCommand(Action action)
- {
- _action = action;
- }
-
- public void Execute(object parameter)
- {
- _action();
- }
-
- public bool CanExecute(object parameter)
- {
- return true;
- }
-
- public event EventHandler CanExecuteChanged
- {
- add { }
- remove { }
- }
- }
-
- /// <summary>
- /// Holds details of a news headline
- /// </summary>
- public class Headline
- {
- public Headline(NewsViewModel owner)
- {
- SelectCommand = new TemporaryDelegateCommand(() => owner.ExpandedHeadline = this);
- }
-
- public string Title { get; set; }
- public string Summary { get; set; }
-
- public ICommand SelectCommand { get; set; }
- }
-
-
- }