Designing WCF DataContract Classes Using the LINQ to SQL Designer - Creating WCF DataContract classes visually using LINQ to SQL Designer: Accessing in Silverlight
(Page 3 of 5 )
In the previous section, we have seen how we can create "DataContract" classes easily using the "LINQ to SQL Designer." Now, the goal is to access the same in a Silverlight application.
Modify IEmpService.vb as follows:
<ServiceContract()> _
Public Interface IEmpService
<OperationContract()> _
Function GetEnameDname() As List(Of EnameDname)
End Interface
Modify "EmpService.vb" as follows:
Imports System.Data.Linq
Public Class EmpService
Implements IEmpService
Public Sub New()
End Sub
Public Function GetEnameDname() As System.Collections.Generic.List(Of EnameDname) Implements IEmpService.GetEnameDname
Using ctxt As New DemoEmpDataContext
ctxt.ObjectTrackingEnabled = False
Return (From p In ctxt.Emps _
Select New EnameDname With {.Ename = p.Ename, .Dname = p.Dept.Dname}).ToList
End Using
End Function
End Class
Modify the Silverlight UI markup as follows:
<UserControl x:Class="DemoSL.Page5"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="400" Height="300" xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data">
<StackPanel Height="Auto" HorizontalAlignment="Stretch" x:Name="LayoutRoot" VerticalAlignment="Stretch" Background="White" Orientation="Vertical">
<Button Height="27" HorizontalAlignment="Left" x:Name="btnShow" Width="80" Content="Show"/>
<TextBlock Height="26" HorizontalAlignment="Left" x:Name="lblMsg" Width="350" Text="" TextWrapping="Wrap"/>
<ScrollViewer Height="243" HorizontalAlignment="Stretch" Width="Auto">
<data:DataGrid x:Name="dgEmpDept"/>
</ScrollViewer>
</StackPanel>
</UserControl>
And finally, make the code-behind as follows:
Partial Public Class Page5
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.GetEnameDnameCompleted, AddressOf EmployeeListFetched
objService.GetEnameDnameAsync()
End Sub
Private Sub EmployeeListFetched(ByVal sender As Object, ByVal e As EmpService.GetEnameDnameCompletedEventArgs)
Me.dgEmpDept.ItemsSource = e.Result
Me.lblMsg.Text = "Fetched!"
End Sub
End Class
Next: Inheriting WCF DataContract classes visually using LINQ to SQL Designer >>
More Windows Scripting Articles
More By Jagadish Chaterjee