New Page 1
The solution demonstrates a very simple example of calling a WCF service from a Silverlight 3 application.
Web Site WCF Service <-> Silverlight 3 Client
The three main elements of the example are the WCF service contained in the examples web project, the XAML of the Silverlight 'MainPage', and the code of the 'Main Page'.
The WCF service:
A Silverlight-enabled WCF Service was added to the web project. This template is available if you are using the latest version of Silverlight 3.
An operation contract, GetCountries, was added.
Imports System.ServiceModel
Imports System.ServiceModel.Activation
<ServiceContract(Namespace:="")> _
<AspNetCompatibilityRequirements(RequirementsMode:=AspNetCompatibilityRequirementsMode.Allowed)> _
Public Class SL3_WCF_Service
<OperationContract()> _
Public Function GetCountries() As List(Of Country)
Dim db As New LightSpeedClassesDataContext
Return (From c In db.Countries Select c).ToList
End Function
End Class
The MainPage's XAML with the ComboBox declaration:
<UserControl x:Class="SL3_WCF_ComboBox.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480">
<Grid x:Name="LayoutRoot">
<ComboBox x:Name="CountryComboBox" Height="30" Width="150"></ComboBox>
</Grid>
</UserControl>
The MainPages code behind:
The code behind:
1. Instantiates a SL3_WCF_Service_Ref.SL3_WCF_ServiceClient object.
2. Adds a handler to receive the results of calling the WCF service.
3. Calls the service's GetCountriesAsync method to request country objects from the server.
4. Assigned the country objects returned to an overservable collection.
5. Binds the observable collection to a ComboBox.
Imports System.Collections.ObjectModel
Partial Public Class MainPage
Inherits UserControl
'Declare and instantiate the WCF service.
Public m_SL3_WCF_Service As SL3_WCF_Service_Ref.SL3_WCF_ServiceClient = New SL3_WCF_Service_Ref.SL3_WCF_ServiceClient
Public Sub New()
InitializeComponent()
LoadCountryComboBox()
End Sub
Private Sub LoadCountryComboBox()
' Add handler for WCF service's GetCountriesCompleted results.
AddHandler m_SL3_WCF_Service.GetCountriesCompleted, AddressOf GetCountriesCompleted
' Call the WCF service's GetCountriesAsync method to call the service and ask for the countries.
m_SL3_WCF_Service.GetCountriesAsync()
End Sub
Private Sub GetCountriesCompleted(ByVal sender As Object, ByVal e As SL3_WCF_Service_Ref.GetCountriesCompletedEventArgs)
' Declare an observable collection to contain the results of the call to the GetCountriesAsync method.
Dim countriesObservableCollection As ObservableCollection(Of SL3_WCF_Service_Ref.Country)
' Assign the List(Of Country) returned by the service to the observable collection.
countriesObservableCollection = e.Result
' Set the CountryComboBox's ItemsSource to the observable collection.
CountryComboBox.ItemsSource = countriesObservableCollection
' Set the CountryComboBox's DisplayMemberPath (what the user will see) to Title (a property of the Country class)
CountryComboBox.DisplayMemberPath = "Title"
' Set the CountryComboBox's SelectedItem to the first country in the countriesObservableCollection.
CountryComboBox.SelectedItem = countriesObservableCollection(0)
End Sub
End Class