The steps are:
1. New Project, select C#/Silverlight/SilverilghtApplication. Click OK to “Add Silverlight Application” dialog, which defaults to “Add a new ASP.NET Web project to the solution to host Silverlight”. I named the project as StockQuotesForBlog.
2. Change page.xaml’s width and height to 800x600, and add the graphical interface into the XAML:
<UserControl xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data" x:Class="StockQuotesForBlog.Page"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="800" Height="600">
<StackPanel Margin="10,10,10,10">
<TextBlock Name="TextBlockQuotes" Text="Please enter a symbol, or symbols seperated by comma"></TextBlock>
<TextBox x:Name="TextBoxQuoteInput" Text="MSFT,GOOG,YHOO" KeyDown="TextBoxQuoteInput_KeyDown"></TextBox>
<StackPanel Orientation="Horizontal" >
<Button Name="ButtonGetQuote" Background="Brown" Content="GetQuote" Click="ButtonGetQuote_Click" ></Button>
</StackPanel>
<data:DataGrid x:Name="dataGridStock"
Margin="0,5,0,10"
RowHeight="44"
Height="500"
VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto" AlternatingRowBackground="LemonChiffon" >
<data:DataGrid.Columns>
<data:DataGridTextColumn
Header="Symbol"
Width="80"
Binding ="{Binding symbol}"
FontSize="12" />
<data:DataGridTextColumn
Header="Company"
Width="120"
Binding="{Binding name}"
FontSize="12" />
<data:DataGridTextColumn
Header="Time"
Width="80"
Binding="{Binding time}"
FontSize="12" />
<data:DataGridTextColumn
Header="Trade"
Width="80"
Binding="{Binding last}"
FontSize="12" />
<data:DataGridTextColumn
Header="Change"
Width="80"
Binding="{Binding change}"
FontSize="12" />
<data:DataGridTextColumn
Header="%Chg"
Width="80"
Binding="{Binding percentageChange}"
FontSize="12" />
<data:DataGridTextColumn
Header="Volume"
Width="80"
Binding="{Binding volume}"
FontSize="12" />
</data:DataGrid.Columns>
</data:DataGrid>
</StackPanel>
</UserControl>
3. Add a code file, Stock service.cs
namespace StockQuotesForBlog
{
public class OneStock
{
public string symbol { get; set; }
public string last { get; set; }
public string date { get; set; }
public string time { get; set; }
public string change { get; set; }
public string open { get; set; }
public string high { get; set; }
public string low { get; set; }
public string volume { get; set; }
public string mktCap { get; set; }
public string previousClose { get; set; }
public string percentageChange { get; set; }
public string annRange { get; set; }
public string earns { get; set; }
public string pe { get; set; }
public string name { get; set; }
private string _lastForegroundColor = "black";
public string lastForegroundColor
{
get
{
return _lastForegroundColor;
}
set
{
_lastForegroundColor = value;
}
}
}
}
4. Add service reference of System.XML.Linq
5. In Page.xaml.cs, we add the following code for the basic functionalities:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Xml;
using System.Xml.Linq;
using System.IO;
using System.IO.IsolatedStorage;
using System.Text;
using System.Collections;
namespace StockQuotesForBlog
{
public partial class Page : UserControl
{
private Char[] m_separators = new Char[] { ' ', ',', ';' };
private Storyboard m_sb = null;
private Dictionary<string, OneStock> m_AllStocks = new Dictionary<string, OneStock>();
private int m_count = 0;
private int m_maxCount = 0;
public Page()
{
InitializeComponent();
TextBoxQuoteInput.Focus();
}
private void RunQuery()
{
string symbols = TextBoxQuoteInput.Text.Trim();
string[] split = symbols.Split(m_separators, System.StringSplitOptions.RemoveEmptyEntries);
m_count = 0;
m_maxCount = split.Count();
foreach (string symbol in split)
{
string url = "http://www.webservicex.net/stockquote.asmx/GetQuote?symbol=" + symbol;
// Initiate Async Network call to Digg
WebClient stockQuoteService = new WebClient();
stockQuoteService.DownloadStringCompleted += new DownloadStringCompletedEventHandler(stockQuoteService_DownloadStringCompleted);
stockQuoteService.DownloadStringAsync(new Uri(url));
}
}
void stockQuoteService_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
if (e.Error == null)
DisplayInDataGrid(e.Result);
}
private void DisplayInDataGrid(string xmlString)
{
XDocument xd1 = XDocument.Parse(xmlString);
xmlString = xd1.Root.Value;
XDocument xmlStories = XDocument.Parse(xmlString);
var stocks = from g in xmlStories.Descendants("Stock")
select new OneStock
{
symbol = g.Element("Symbol").Value,
last = g.Element("Last").Value,
date = g.Element("Date").Value,
time = g.Element("Time").Value,
change = g.Element("Change").Value,
open = g.Element("Open").Value,
high = g.Element("High").Value,
low = g.Element("Low").Value,
volume = g.Element("Volume").Value,
mktCap = g.Element("MktCap").Value,
previousClose = g.Element("PreviousClose").Value,
percentageChange = g.Element("PercentageChange").Value,
annRange = g.Element("AnnRange").Value,
earns = g.Element("Earns").Value,
pe = g.Element("P-E").Value,
name = g.Element("Name").Value,
};
m_count++;
foreach (OneStock stock in stocks)
{
if (m_AllStocks.ContainsKey(stock.symbol))
{
m_AllStocks[stock.symbol].last = stock.last;
m_AllStocks[stock.symbol].date = stock.date;
m_AllStocks[stock.symbol].time = stock.time;
m_AllStocks[stock.symbol].change = stock.change;
m_AllStocks[stock.symbol].open = stock.open;
m_AllStocks[stock.symbol].high = stock.high;
m_AllStocks[stock.symbol].low = stock.low;
m_AllStocks[stock.symbol].volume = stock.volume;
m_AllStocks[stock.symbol].mktCap = stock.mktCap;
m_AllStocks[stock.symbol].previousClose = stock.previousClose;
m_AllStocks[stock.symbol].percentageChange = stock.percentageChange;
m_AllStocks[stock.symbol].annRange = stock.annRange;
m_AllStocks[stock.symbol].earns = stock.earns;
m_AllStocks[stock.symbol].pe = stock.pe;
m_AllStocks[stock.symbol].lastForegroundColor = stock.lastForegroundColor;
}
else
{
m_AllStocks.Add(stock.symbol, stock);
}
}
BindItemSource();
}
private void BindItemSource()
{
if (m_count >= m_maxCount)
{
int x = dataGridStock.SelectedIndex;
dataGridStock.ItemsSource = null;
dataGridStock.ItemsSource = m_AllStocks.Values;
dataGridStock.SelectedIndex = x;
//remove the extra columns in the table
while (dataGridStock.Columns.Count > 7)
{
dataGridStock.Columns.RemoveAt(7);
}
}
}
private void ButtonGetQuote_Click(object sender, RoutedEventArgs e)
{
RunQuery();
StartRefresh();
}
private void TextBoxQuoteInput_KeyDown(object sender, KeyEventArgs e)
{
switch (e.Key)
{
case Key.Enter:
RunQuery();
StartRefresh();
e.Handled = true;
break;
default:
e.Handled = false;
break;
}
}
private void StartRefresh()
{
if (m_sb == null)
{
m_sb = new Storyboard();
m_sb.Completed += Storyboard_Completed;
Duration duration = new Duration(TimeSpan.FromSeconds(10));
m_sb.Duration = duration;
}
m_sb.Begin();
}
private void Storyboard_Completed(object sender, EventArgs e)
{
RunQuery();
m_sb.Begin();
}
}
}
6. Ctrl F5, and click GetQuote Button, to see the basic query in action.