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

Language C# Lines 167
MD5 Hash f8254ec9a88578e363a9e2eb3e0f8cfe Estimated Cost $3,197 (why?)
Repository https://bitbucket.org/suhas_28/sharpfellows.toolkit View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Reactive.Concurrency;
using System.IO;
using System.Linq;
using System.Net;
using System.Reactive.Linq;
using System.Text;
using System.Windows;
using System.Windows.Input;
using System.Xml.Linq;

namespace Marquee_Silverlight
{
    public class NewsViewModel : INotifyPropertyChanged
    {
        private IDisposable _observation;
        private Headline _expandedHeadline;

        public NewsViewModel()
        {
            Headlines = new ObservableCollection<Headline>();

            // The Silverlight permissions model places restrictions on which sites we can access if we are hosted in the browser.
            string feedUrl = null;
            if (Application.Current.IsRunningOutOfBrowser)
            {
                feedUrl = "http://feeds.bbci.co.uk/news/rss.xml";
            }

            if (!string.IsNullOrEmpty(feedUrl))
            {
                var webRequest = WebRequest.Create(feedUrl);

                _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(Scheduler.CurrentThread)
                    .Subscribe(headline => Headlines.Add(headline), // And add the object into the bound collection
                               ex => Headlines.Add(new Headline(this) // Render exception details into the marquee
                                                       {
                                                           Title =
                                                               String.IsNullOrEmpty(ex.Message)
                                                                   ? ex.GetType().FullName
                                                                   : ex.Message
                                                       }));
            }
            else
            {
                Headlines.Add(new Headline(this)
                                  {
                                      Title = "Welcome to the MVVM-compatible Marquee control.",
                                      Summary = "Unfortunately Silverlight security policies stop us from downloading an interesting content right now. Try running this out-of-browser to access BBC headlines."
                                  });
                Headlines.Add(new Headline(this)
                                  {
                                      Title = "How to run out-of-browser?",
                                      Summary = "Right click on this marquee and choose 'Install onto this computer'"
                                  });
                Headlines.Add(new Headline(this)
                                  {
                                      Title = "This doesn't look much like a marquee control.",
                                      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."
                                  });
                Headlines.Add(new Headline(this)
                                  {
                                      Title = "What makes it MVVM compatible?",
                                      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."
                                  });
            }
        }

        /// <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; }
    }

    
}
Back to Top