Silverlight 2.0 Application Development with LINQ to SQL and a WCF Service - Silverlight development
(Page 4 of 6 )
Now we need to develop a Silverlight 2 application which consumes the previously mentioned WCF Service. You need to take the following steps.
Add a new “Silverlight Application” project to the existing solution and name it “DemoSL.” You should get two projects, “DemoSL” and “DemoSL.Web” (as explained in my previous article).
Configure the virtual path of “DemoSL.Web” to point to your local IIS (just as you did in the previous section).
Add “Service Reference” to the “WCF Service” project available within your solution.
Go to File || Add || New Project (to the existing solution).
Configure “ServiceReferences.ClientConfig” in “DemoSL” accordingly (explained in previous article). Rebuild the solution.
Modify your code in Page.xaml, so that it looks like the following:
<UserControl x:Class="DemoSL.Page"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="400" Height="300" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data">
<Grid x:Name="LayoutRoot" Background="White">
<Grid.RowDefinitions>
<RowDefinition Height="0.15*"/>
<RowDefinition Height="0.25*"/>
<RowDefinition Height="0.60*"/>
</Grid.RowDefinitions>
<Button HorizontalAlignment="Left" Margin="5,5,0,0" VerticalAlignment="Stretch" Width="76" Content="Show" x:Name="btnShow" Height="30" />
<TextBlock Margin="5,5,0,0" VerticalAlignment="Stretch" Text="" TextWrapping="Wrap" x:Name="lblMsg" Height="22" Grid.Row="1" />
<data:DataGrid x:Name="dgEmployees" AutoGenerateColumns="True" Grid.Row="2"></data:DataGrid>
</Grid>
</UserControl>
Once the datagrid control is added to the Silverlight application, you should have an assembly named “System.Windows.Controls.Data” added to your reference list (if not, you should add it manually). You should also observe the namespace “my” added to the “UserControl” tag.
Go to the code-behind of “Page.xaml” and modify the code to look like the following:
Partial Public Class Page
Inherits UserControl
Public Sub New()
InitializeComponent()
End Sub
Private Sub btnShow_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnShow.Click
Me.lblMsg.Text = "Fetching..."
Dim objService As New EmpService.EmpServiceClient
AddHandler objService.GetEmployeeListCompleted, AddressOf EmployeeListFetched
objService.GetEmployeeListAsync()
End Sub
Private Sub EmployeeListFetched(ByVal sender As Object, ByVal e As EmpService.GetEmployeeListCompletedEventArgs)
Me.dgEmployees.ItemsSource = e.Result
Me.lblMsg.Text = "Fetched!"
End Sub
End Class
Once you execute your Silverlight application, you should see the output as follows:

Next: Troubleshooting configurations >>
More Windows Scripting Articles
More By Jagadish Chaterjee