PageRenderTime 52ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/Samples/Controls/Marquee/Silverlight/Marquee-Silverlight/NewsViewModel.cs

https://bitbucket.org/bu5hm4nn/sharpfellows.toolkit
C# | 166 lines | 131 code | 20 blank | 15 comment | 4 complexity | f8254ec9a88578e363a9e2eb3e0f8cfe MD5 | raw file
  1. using System;
  2. using System.Collections.ObjectModel;
  3. using System.ComponentModel;
  4. using System.Reactive.Concurrency;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Net;
  8. using System.Reactive.Linq;
  9. using System.Text;
  10. using System.Windows;
  11. using System.Windows.Input;
  12. using System.Xml.Linq;
  13. namespace Marquee_Silverlight
  14. {
  15. public class NewsViewModel : INotifyPropertyChanged
  16. {
  17. private IDisposable _observation;
  18. private Headline _expandedHeadline;
  19. public NewsViewModel()
  20. {
  21. Headlines = new ObservableCollection<Headline>();
  22. // The Silverlight permissions model places restrictions on which sites we can access if we are hosted in the browser.
  23. string feedUrl = null;
  24. if (Application.Current.IsRunningOutOfBrowser)
  25. {
  26. feedUrl = "http://feeds.bbci.co.uk/news/rss.xml";
  27. }
  28. if (!string.IsNullOrEmpty(feedUrl))
  29. {
  30. var webRequest = WebRequest.Create(feedUrl);
  31. _observation = Observable.FromAsyncPattern<WebResponse>(webRequest.BeginGetResponse,
  32. webRequest.EndGetResponse)()
  33. // Execute the web request to get XML
  34. .Select(response => response.GetResponseStream())
  35. .Select(stream =>
  36. {
  37. using (var reader = new StreamReader(stream, Encoding.UTF8))
  38. return reader.ReadToEnd();
  39. })
  40. .Select(xml => XDocument.Parse(xml)) // Parse the XML
  41. .SelectMany(document => document.Descendants("channel").Descendants("item").ToObservable())
  42. // Extract the item elements
  43. .Select(xelem => new Headline(this) // Convert from XML into our model class
  44. {
  45. Title = xelem.Descendants("title").First().Value,
  46. Summary = xelem.Descendants("description").First().Value,
  47. })
  48. .ObserveOn(Scheduler.CurrentThread)
  49. .Subscribe(headline => Headlines.Add(headline), // And add the object into the bound collection
  50. ex => Headlines.Add(new Headline(this) // Render exception details into the marquee
  51. {
  52. Title =
  53. String.IsNullOrEmpty(ex.Message)
  54. ? ex.GetType().FullName
  55. : ex.Message
  56. }));
  57. }
  58. else
  59. {
  60. Headlines.Add(new Headline(this)
  61. {
  62. Title = "Welcome to the MVVM-compatible Marquee control.",
  63. Summary = "Unfortunately Silverlight security policies stop us from downloading an interesting content right now. Try running this out-of-browser to access BBC headlines."
  64. });
  65. Headlines.Add(new Headline(this)
  66. {
  67. Title = "How to run out-of-browser?",
  68. Summary = "Right click on this marquee and choose 'Install onto this computer'"
  69. });
  70. Headlines.Add(new Headline(this)
  71. {
  72. Title = "This doesn't look much like a marquee control.",
  73. Summary = "Perhaps your browser is too big. As you resize it the marquee will decrease in size. Once the visible window is smaller than the content, it will start to scroll."
  74. });
  75. Headlines.Add(new Headline(this)
  76. {
  77. Title = "What makes it MVVM compatible?",
  78. Summary = "The content is driven purely by databinding onto a view-model and the scrolling action is driven by the size of the content. So there is no need for code-behind."
  79. });
  80. }
  81. }
  82. /// <summary>
  83. /// The headline which currently has its summary displayed
  84. /// </summary>
  85. public Headline ExpandedHeadline
  86. {
  87. get
  88. {
  89. return _expandedHeadline;
  90. }
  91. set
  92. {
  93. _expandedHeadline = value;
  94. OnPropertyChanged("ExpandedHeadline");
  95. }
  96. }
  97. /// <summary>
  98. /// List of all headlines
  99. /// </summary>
  100. public ObservableCollection<Headline> Headlines { get; private set; }
  101. public event PropertyChangedEventHandler PropertyChanged;
  102. public void OnPropertyChanged(string propertyName)
  103. {
  104. PropertyChangedEventHandler handler = PropertyChanged;
  105. if (handler != null)
  106. handler(this, new PropertyChangedEventArgs(propertyName));
  107. }
  108. }
  109. /// <summary>
  110. /// 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.
  111. /// </summary>
  112. public class TemporaryDelegateCommand : ICommand
  113. {
  114. private Action _action;
  115. public TemporaryDelegateCommand(Action action)
  116. {
  117. _action = action;
  118. }
  119. public void Execute(object parameter)
  120. {
  121. _action();
  122. }
  123. public bool CanExecute(object parameter)
  124. {
  125. return true;
  126. }
  127. public event EventHandler CanExecuteChanged
  128. {
  129. add { }
  130. remove { }
  131. }
  132. }
  133. /// <summary>
  134. /// Holds details of a news headline
  135. /// </summary>
  136. public class Headline
  137. {
  138. public Headline(NewsViewModel owner)
  139. {
  140. SelectCommand = new TemporaryDelegateCommand(() => owner.ExpandedHeadline = this);
  141. }
  142. public string Title { get; set; }
  143. public string Summary { get; set; }
  144. public ICommand SelectCommand { get; set; }
  145. }
  146. }