Source Code and Snippets for my one-hour screencast–Phone and Cloud–An natural pair of technologies
|
I recently posted a huge, one-hour screencast of building mobile cloud applications, leveraging SQL Azure as a data source.
You can see the full video here:
https://www.virtualtechsummits.com/Register.aspx?EventID=5 I chose orange for the ticker symbol because the San Francisco Giants won the World Series. Email me if you would like access to the video at bterkaly@microsoft.com One of the the applications will look like this: |
|
There is also a Windows Phone 7 Project here: |
|
Snippet 1 - Using Statement in WCF Service |
using TierData;
using System.Collections.Generic;
|
Snippet 2 - WCF Service Method = GetAssetPrices() |
[OperationContract]
public List<TierDataLayer.MarketIndexPrices> GetAssetPrices(string ticker)
{
MarketIndexDataEntities _db =
new MarketIndexDataEntities();
var ms = (from ap in _db.AssetPrices
where ap.Asset.AssetID == ticker
select new MarketIndexPrices
{
AssetID = ap.Asset.AssetID,
DatePrice = ap.DatePrice,
OpenPrice = ap.OpenPrice
});
return ms.ToList();
}
|
Snippet 3 – Class = MarketIndexPrices.cs in TierDataLayer |
[DataContract]
public class MarketIndexPrices
{
public MarketIndexPrices() { }
[DataMember]
public string AssetID;
[DataMember]
public DateTime DatePrice;
[DataMember]
public double OpenPrice;
}
|
Snippet 4 – Class MyFormatter.cs at TierVisualLayer and WP7 |
public class MyFormatter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (parameter != null)
{
string format_string = parameter.ToString();
if(!string.IsNullOrEmpty(format_string))
return string.Format(culture, format_string, value);
}
return value.ToString();
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
|
Snippet 5 – User Control XAML for Formatting Code |
<UserControl.Resources>
<converter:MyFormatter x:Key="FormatConverter" />
</UserControl.Resources>
|
Snippet 6 – Make sure main XAML Grid is “Black” |
<Grid x:Name="LayoutRoot" Background="Black">
</Grid>
|
Snippet 7 – Row and Header Definitions for MainPage.xaml in TierWebLayer |
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid x:Name="TitleGrid" Grid.Row="0">
<StackPanel>
<TextBlock Text="Stock Market Data" x:Name="textBlockPageTitle" Foreground="#FFEFEF12" FontSize="32" />
<TextBlock Text="Search Screen" x:Name="textBlockListTitle" Foreground="White" FontSize="28" />
</StackPanel>
</Grid>
|
Snippet 8 – Main grid for MainPage.xaml in TierWebLayer |
<Grid x:Name="ContentGrid" Grid.Row="1" Background="Black">
<Grid.RowDefinitions>
<RowDefinition Height="10"/>
<RowDefinition Height="70" />
<RowDefinition Height="*" />
<RowDefinition Height="10" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="10" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="10" />
</Grid.ColumnDefinitions>
<!--Next code block goes here-->
<Border BorderBrush="Black" BorderThickness="2"
Grid.Row="1" Grid.Column="1"/>
<StackPanel Name="MyStackPanel" Grid.Row="1" Grid.Column="1" Orientation="Horizontal" Background="Black" Grid.ColumnSpan="2" Margin="1,0,9,0">
<TextBlock Text="Ticker Symbol " VerticalAlignment="Center"
FontSize="22" Margin="15,0,0,0" Foreground="white"/>
<TextBox x:Name="txtSearch" Height="32" Margin="2,0,0,4"
VerticalAlignment="Center"
FontWeight="ExtraBold" Width="142"
/>
<Button x:Name="Search" Margin="20,0,0,4" Content="Search"
VerticalAlignment="Center" FontWeight="Bold"
FontSize="14" HorizontalAlignment="Right" Click="Search_Click" Height="30" Width="87" />
</StackPanel>
<ListBox Name="myListBox" Background="Black" Grid.Column="1" Grid.Row="2"
Height="100" HorizontalAlignment="Left" Margin="2,6,0,0"
VerticalAlignment="Top" Width="480" Visibility="Collapsed" MinHeight="400">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=AssetID}" Foreground="Orange" FontSize="18" Width="80"/>
<TextBlock Text="{Binding Path=DatePrice, Converter={StaticResource FormatConverter},
ConverterParameter=\{0:MM/dd/yyyy\}}" Foreground="White" FontSize="18" Width="150"/>
<TextBlock Text=" - "></TextBlock>
<TextBlock Text="{Binding Path=OpenPrice, Converter={StaticResource FormatConverter},
ConverterParameter=\{0:0.00\}}" Foreground="Aqua" FontSize="18"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
|
Snippet 9 – Search event procedure with callback (MainPage.xaml.cs) |
private void Search_Click(object sender, RoutedEventArgs e)
{
ServiceStocksClient client =
new ServiceStocksClient();
client.GetAssetPricesCompleted +=
new EventHandler<GetAssetPricesCompletedEventArgs>(
client_GetAssetPricesCompleted);
client.GetAssetPricesAsync(this.txtSearch.Text);
}
void client_GetAssetPricesCompleted(object sender, GetAssetPricesCompletedEventArgs e)
{
this.myListBox.Visibility = System.Windows.Visibility.Visible;
if (e.Result != null)
{
this.myListBox.ItemsSource = e.Result;
}
}
|
Snippet 10 – Using statement in MainPage.xaml |
using TierVisual.ServiceStocks;
|
Snippet 11 – Database address |
|
Yours will be different. Go to the portal to get your database address. |
bjkekaxsxp9c.database.windows.net
brunoterkal@bjkekaxsxp9c
// You get your user name from the portal when you sign up for SQL Azure
|
Snippet 13 – Password – You get that from the portal |
// I can't give you mine :-)
|
Snippet 14 – Update statement used in demo |
update assetprices set openprice = 1000000 where assetpricesid = 1