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

Language C# Lines 123
MD5 Hash aab81164e88d0b01b8cf0115e79ce251 Estimated Cost $2,181 (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
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; }
    }

    
}
Back to Top