Click here to Skip to main content
15,896,726 members
Articles / Mobile Apps / Windows Phone 7

XAMLFinance – A Cross-platform WPF, Silverlight & WP7 Application

Rate me:
Please Sign up or sign in to vote.
4.94/5 (99 votes)
21 Sep 2011CPOL27 min read 218K   9.9K   251  
This article describes the development of XAML Finance, a cross-platform application which works on the desktop, using Windows Presentation Foundation (WPF), on the web, using Silverlight and on Windows Phone 7 (WP7).
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using System.Xml.Linq;
using System.Linq;
using System.Windows.Resources;
using System.IO;

namespace SLUGUK.ViewModel
{
  public static class AtomConst
  {
    private static string AtomNamespace = "http://www.w3.org/2005/Atom";

    public static XName Entry = XName.Get("entry", AtomNamespace);

    public static XName ID = XName.Get("id", AtomNamespace);

    public static XName Link = XName.Get("link", AtomNamespace);

    public static XName Published = XName.Get("published", AtomNamespace);

    public static XName Name = XName.Get("name", AtomNamespace);

    public static XName Title = XName.Get("title", AtomNamespace);
  }

  public class FeedViewModel 
  {
    // Twitter search for #Silverlight
    private readonly string _twitterUrl = "http://search.twitter.com/search.atom?rpp=100&&q=%23Silverlight";

    private WebClient _webClient = new WebClient();

    private FeedItemViewModelCollection _feedItems = new FeedItemViewModelCollection();

    public FeedViewModel()
    {
      
    }
    
    /// <summary>
    /// Parses the response from our twitter request, creating a list of FeedItemViewModelinstances
    /// </summary>
    private void ParseXMLResponse(string xml)
    {
      var doc = XDocument.Parse(xml);
      var items = doc.Descendants(AtomConst.Entry)
                      .Select(entryElement => new FeedItemViewModel()
                      {
                        Title = entryElement.Descendants(AtomConst.Title).Single().Value,
                        Id = long.Parse(entryElement.Descendants(AtomConst.ID).Single().Value.Split(':')[2]),
                        Timestamp = DateTime.Parse(entryElement.Descendants(AtomConst.Published).Single().Value),
                        Author = entryElement.Descendants(AtomConst.Name).Single().Value
                      });

      _feedItems.Clear();
      foreach (var item in items)
      {
        _feedItems.Add(item);
      }
    }

    /// <summary>
    /// Gets the feed items
    /// </summary>
    public FeedItemViewModelCollection FeedItems
    {
      get { return _feedItems; }
      set { _feedItems = value; }
    }

    /// <summary>
    /// Gets a feed item by its Id
    /// </summary>
    public FeedItemViewModel GetFeedItem(long id)
    {
      return _feedItems.SingleOrDefault(item => item.Id == id);
    }
    
    /// <summary>
    /// Update the feed items
    /// </summary>
    public void Update()
    {
      _webClient.DownloadStringCompleted += (s, e) => ParseXMLResponse(e.Result);
      _webClient.DownloadStringAsync(new Uri(_twitterUrl));
      
      //ParseXMLResponse(ReadFileToString("data.xml"));
    }



    private string ReadFileToString(string filename)
    {

#if !SILVERLIGHT

      using (var stream = this.GetType().Assembly.GetManifestResourceStream(
        "SLUGUK.ViewModel." + filename))
      {
        StreamReader reader = new StreamReader(stream);
        string xml = reader.ReadToEnd();
        return xml;
      }

#else

      string path = "/SilverlightTwitterApp;component/ViewModel/" + filename;
      Uri uri = new Uri(path, UriKind.Relative);
      StreamResourceInfo sri = Application.GetResourceStream(uri);
      StreamReader reader = new StreamReader(sri.Stream);
      string xml = reader.ReadToEnd();
      return xml;

#endif
    }
  }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect Scott Logic
United Kingdom United Kingdom
I am CTO at ShinobiControls, a team of iOS developers who are carefully crafting iOS charts, grids and controls for making your applications awesome.

I am a Technical Architect for Visiblox which have developed the world's fastest WPF / Silverlight and WP7 charts.

I am also a Technical Evangelist at Scott Logic, a provider of bespoke financial software and consultancy for the retail and investment banking, stockbroking, asset management and hedge fund communities.

Visit my blog - Colin Eberhardt's Adventures in .NET.

Follow me on Twitter - @ColinEberhardt

-

Comments and Discussions