A group blog from members of the VB team
In this walkthrough, I will demonstrate how to convert an existing Windows Forms application that consumes data from a Windows Communication Foundation (WCF) service to Silverlight. Also in the process of conversion we will ensure that the existing functionality is preserved.
Here are some topics that we will cover:
Before you begin you need to download the offline kit from the Firestarter Labs, to use the existing applications.
You can migrate the Windows Forms application to Silverlight in three simple steps as follows:
Let’s take a look at the code that we’ll be migrating. To start exploring the Visual Basic Windows Forms application, follow the following steps:
Now let’s migrate the application to Silverlight. We'll create a new Silverlight project, work with XAML, create a WCF service proxy to interact with the service, and design a user interface that mirrors the existing Windows Forms user interface.
<UserControl x:Class="SilverlightCustomerViewer.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:DesignHeight="545" d:DesignWidth="550" Width="545" Height="550">
<Grid x:Name="LayoutRoot" Background="White">
</Grid>
</UserControl>
Now let’s create a WCF service proxy that can be used to call an existing WCF service. You'll also use a clientaccesspolicy.xml file to handle cross domain issues and bind data to controls.
Namespace CustomerService.Proxies
Partial Public Class Customer
Public ReadOnly Property FullName() As String
Get
Return FirstName & " " & LastName
End Get
End Property
End Class
Imports CustomerService.Proxies
AddHandler Loaded, AddressOf MainPage_Loaded
Private Sub MainPage_Loaded(ByVal sender As Object, ByVal e As RoutedEventArgs)
Dim proxy = New CustomerServiceClient()
AddHandler proxy.GetCustomersCompleted, AddressOf proxy_GetCustomersCompleted
proxy.GetCustomersAsync()
End Sub
Private Sub proxy_GetCustomersCompleted(ByVal sender As Object, ByVal e As GetCustomersCompletedEventArgs)
CustomersComboBox.ItemsSource = e.Result
Text="{Binding ElementName=CustomersComboBox, Path=SelectedItem.CustomerID }”
<TextBox Height="23" HorizontalAlignment="Left" Margin="158,225,0,0" VerticalAlignment="Top" Width="219" Text="{Binding ElementName=CustomersComboBox, Path=SelectedItem.FirstName,Mode=TwoWay}" DataContext="{Binding}" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="158,270,0,0" VerticalAlignment="Top" Width="219" Text="{Binding ElementName=CustomersComboBox, Path=SelectedItem.LastName,Mode=TwoWay}" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="158,316,0,0" VerticalAlignment="Top" Width="219" Text="{Binding ElementName=CustomersComboBox, Path=SelectedItem.CompanyName,Mode=TwoWay}"/>
<TextBox Height="23" HorizontalAlignment="Left" Margin="158,366,0,0" VerticalAlignment="Top" Width="219" Text="{Binding ElementName=CustomersComboBox, Path=SelectedItem.EmailAddress,Mode=TwoWay}" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="158,416,0,0" VerticalAlignment="Top" Width="219" Text="{Binding ElementName=CustomersComboBox, Path=SelectedItem.Phone,Mode=TwoWay}" />
Dim cust = TryCast(CustomersComboBox.SelectedItem, Customer)
cust.ChangeTracker.State = ObjectState.Modified
AddHandler proxy.SaveCustomerCompleted, Sub(s, args)
Dim opStatus = args.Result
Dim msg As String = If(opStatus.Status, "Customer Updated!", "Unable to update Customer: " & opStatus.Message)
MessageBox.Show(msg)
proxy.SaveCustomerAsync(cust)
cust.ChangeTracker.State = ObjectState.Deleted
Dim opStatus As OperationStatus = args.Result
If opStatus.Status Then
CType(CustomersComboBox.ItemsSource, ObservableCollection(Of Customer)).Remove(cust)
MessageBox.Show("Customer deleted!")
Else
MessageBox.Show("Unable to delete Customer: " & opStatus.Message)
End If
Thus, in this walkthrough you have examined an existing Windows Forms application and supporting data access and service layers. You have now successfully migrated the existing functionality from the Windows Forms application to Silverlight.
You can find the full source code for the application here.
This is a great article if you are willing to do all this by yourself. Artinsoft already has develop a tool to automate this migration, see www.silverlightmigration.com