Beginning Silverlight 2.0 Development using Visual Studio 2008

This is my first article in a series focusing on Silverlight 2.0 (beta 2) development using Visual Studio 2008. This article is for beginners who are quite new to Silverlight development and need a practical introduction to developing Silverlight applications using Visual Studio 2008.

Contributed by
Rating: 5 stars5 stars5 stars5 stars5 stars / 6
October 29, 2008
Rate this Article:
MEH MEH++


SEARCH ASP FREE
TOOLS YOU CAN USE

advertisement

My development machine is equipped with the following environment before installing any Silverlight-related software or tools:

  • Windows Server 2003 with SP2

  • IIS

  • Visual Studio 2008 (comes with .NET 3.5) with SP1


Before you start, you should have following installed on your machine:

Microsoft Silverlight Tools Beta 2 for Visual Studio 2008 (http://www.microsoft.com/downloads/details.aspx?FamilyId=50A9EC01-267B-4521-B7D7-C0DBA8866434&displaylang=en )

The above mainly installs the following (beta 2):

  • Silverlight 2

  • Silverlight 2 SDK

  • Silverlight Tools for Visual Studio 2008

The entire source code for this article is available in the form of a free downloadable zip file. The solution was developed using Microsoft Visual Studio 2008 Team Edition (with SP1) on Microsoft Windows Server 2003 Standard Edition with Silverlight 2.0 Beta 2. I didn’t really test it in any other environment. I request that you post in the discussion area if you have any problems in execution.

Developing your first Silverlight 2 application

Once you have the necessary software installed (as discussed in the previous section), you are ready to develop a new Silverlight 2 application from scratch!

The following are the steps you need to take to get started:

  • Open Visual Studio 2008

  • Go to File || New Project

  • Select "Silverlight" for the “Project Type,” “Silverlight Application” for the "Template," “FirstSilverlightApp” for the "Name" and finally click on “OK” (as shown in Fig 01).

  • You will be provided with the “Add Silverlight Application” dialog.

  • Select “Web Application Project” for the "Project Type,"  “FirstSilverlightAppWeb” for the "Name" and finally click on “OK” (as shown in Fig 02).

  • Once the solution gets created, you should see a “Solution Explorer” similar to the following (Fig 03).


If you observe the “Solution explorer,” you will find two projects. One is the Silverlight application itself and the other is simply a web application which is used to render and test the Silverlight application developed.

Within the web application project, you should be able to find the folder name “ClientBin.” This mainly contains our Silverlight application in a compiled zip format file named “.xap” (pronounced “zap” file). This gets automatically populated (at run time) based on the configuration as shown below (which is the default; see Fig 04 and Fig 05):

Coding your first Silverlight 2 application

This continues the previous section. In the previous section, we created a new Silverlight application. In this section, we will develop some code and execute the application.

The first step is to create the XAML markup, which simply contains a button and a textbox. Open Page.xaml (available in FirstSilverlightApp) and modify the existing markup so that it looks like the following:


<UserControl x:Class="TestSilverlight.Page"

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

Width="400" Height="300">


 <Canvas Margin="30,26,31,34">

 <Button Height="31" Width="113" Canvas.Left="102" Canvas.Top="21" Content="Disp Message" x:Name="btnShow" Click="btnShow_Click"/>

 <TextBox Height="25" Width="168" Canvas.Left="71" Canvas.Top="82" Text="" TextWrapping="Wrap" Background="#FFFFFFFF" x:Name="txtFirst"/>

 </Canvas>



</UserControl>


Open the code behind (Page.xaml.vb) and modify it so that it matches the following:


Partial Public Class Page

Inherits UserControl


Public Sub New()

InitializeComponent()

End Sub


Private Sub btnShow_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)

Me.txtFirst.Text = "Hello world"

End Sub

End Class


After completing all of the above steps, execute the application and test the button. It should look like the following (Fig 06):

You can also modify the properties of a control dynamically. This is explained in the next section.

Explaining the code

Let us try to understand the previously tested project. If you open Page.xaml, you should see a “Canvas” object as follows:


 <Canvas Margin="30,26,31,34">

 </Canvas>


Canvas is simply a layout control. A layout control acts as a container to hold a few more child controls along with size and positioning. There exist a few more layout controls in Silverlight like Grid, StackPanel etc. I will cover those in my upcoming articles.

The Button control gets created using the following markup:


 <Button Height="31" Width="113" Canvas.Left="102" Canvas.Top="21" Content="Disp Message" x:Name="btnShow" Click="btnShow_Click"/>


The name of the button is defined by using the “x:Name” attribute. Its positioning is currently defined with respect to its container (Canvas). It is currently associated with a click event handler named “btnShow_Click.”

The Textbox control gets created using the following markup:


<TextBox Height="25" Width="168" Canvas.Left="71" Canvas.Top="82" Text="" TextWrapping="Wrap" Background="#FFFFFFFF" x:Name="txtFirst"/>


This is also very similar to the previous Button control except that it is defined with its own background color and name. As we currently don’t need to handle any event with respect to this control, we simply don’t mention any of its events.

When the button is hit, it executes the following code:


Private Sub btnShow_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)

Me.txtFirst.Text = "Hello world"

End Sub


The above code is quite clear. It simply assigns the message “Hello world” to the text box named “txtFirst.”

You can also dynamically modify the properties of a control dynamically as shown below:


Private Sub btnShow_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)


Me.txtFirst.Background = New SolidColorBrush(Colors.Blue)

Me.txtFirst.Foreground = New SolidColorBrush(Colors.Yellow)

Me.txtFirst.Text = "Hello world"


End Sub


Once you execute the above, you should have the following output (Fig 07):

Basic Data Binding in Silverlight 2.0


Let us now deal with data binding using Silverlight 2.0. Create a new Silverlight solution and add a new class (to Silverlight project) as follows:


Public Class Emp


Private _Empno As String

Public Property Empno() As String

Get

Return _Empno

End Get

Set(ByVal value As String)

_Empno = value

End Set

End Property


Private _Ename As String

Public Property Ename() As String

Get

Return _Ename

End Get

Set(ByVal value As String)

_Ename = value

End Set

End Property


End Class


Modify Page.xaml to look as follows:


<UserControl x:Class="TestSilverlight.Page"

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

Width="400" Height="300">

 

 <Canvas Margin="30,26,31,34">

 <Button Height="23" Width="107" Canvas.Left="43" Canvas.Top="8" Content="Bind Now" x:Name="Bind" Click="btnBind_Click"/>

 <TextBlock Height="20" Width="107" Canvas.Left="43" Canvas.Top="51" Text="Empno:" TextWrapping="Wrap"/>

 <TextBox Height="20" Width="142" Canvas.Left="170" Canvas.Top="52.269" Text="{Binding Empno}" TextWrapping="Wrap" x:Name="txtEmpno"/>

 <TextBlock Height="20" Width="107" Text="Ename:" TextWrapping="Wrap" Canvas.Left="43" Canvas.Top="91.269"/>

 <TextBox Height="20" x:Name="txtEname" Width="142" Text="{Binding Ename}" TextWrapping="Wrap" Canvas.Left="170" Canvas.Top="91.269"/>

 </Canvas>

 

</UserControl>


Modify Page.xaml.vb to look as follows:


Partial Public Class Page

Inherits UserControl


Public Sub New()

InitializeComponent()

End Sub


Private Sub btnBind_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)


Dim objEmp As New Emp With {.Empno = "1001", .Ename = "Jag"}

Me.txtEmpno.DataContext = objEmp

Me.txtEname.DataContext = objEmp


End Sub


End Class


Once you execute the above, you should have output as shown below (Fig 08):

The next section gives you an understanding of the code above.

Basic Data Binding in Silverlight 2.0: explanation

Basically, Silverlight applications execute and run on the client side (browser). No code is executed at the web server level. If Silverlight applications were executed at the server level, we'd simply have another ASP.NET!

As Silverlight applications execute at the browser level, we are left with no way to communicate with databases directly. So, we will not be able to bind to databases directly. The best options for getting the data from databases are through either web services or WCF (Windows Communication Foundation) services.

Web Services or WCF components serialize objects (data) and throw them to browser. Once the browser receives them, Silverlight can automatically pick them up and bind the same to Silverlight controls. That is the reason we will be working and binding objects to controls for all kinds of Silverlight applications.

In the previous section, we defined a simple class as follows:


Public Class Emp


Private _Empno As String

Public Property Empno() As String

Get

Return _Empno

End Get

Set(ByVal value As String)

_Empno = value

End Set

End Property


Private _Ename As String

Public Property Ename() As String

Get

Return _Ename

End Get

Set(ByVal value As String)

_Ename = value

End Set

End Property


End Class


The above class simply encapsulates employee number and employee name information. Once the class is defined, we need to instantiate and bind it to Silverlight controls. Currently, to make this article simple, I instantiated the above class with the following statement (along with some sample data):


Dim objEmp As New Emp With {.Empno = "1001", .Ename = "Jag"}


The binding in between the instantiated object and text boxes is made possible using the “Datacontext” property of the controls themselves:


Me.txtEmpno.DataContext = objEmp

Me.txtEname.DataContext = objEmp


We are assigning the same object to both text boxes. But the data gets displayed differently in each of the text boxes. The following declarations made it happen:

 <TextBox Height="20" Width="142" Canvas.Left="170" Canvas.Top="52.269" Text="{Binding Empno}" TextWrapping="Wrap" x:Name="txtEmpno"/>

 <TextBox Height="20" x:Name="txtEname" Width="142" Text="{Binding Ename}" TextWrapping="Wrap" Canvas.Left="170" Canvas.Top="91.269"/>


The most important attribute on which to concentrate is the “Text” property of TextBox. You can observe that the binding specification is provided with the keyword “Binding” together with the property (of Emp object) to which it needs to bind. It is important to make sure that this information is embedded in braces.

In my upcoming articles, we will see more and more examples of Silverlight 2.0 development. I hope you enjoyed the article and any suggestions, bugs, errors, enhancements etc. are highly appreciated at http://jagchat.spaces.live.com

blog comments powered by Disqus
BRAINDUMP ARTICLES

- Microsoft Windows 8 Committed to Cloud Compu...
- Independent Developers Favor Windows Phone 7
- Dell Introduces VMware-based Cloud
- Microsoft and Skype Agree to Acquisition Deal
- Transfer Contacts in Microsoft Outlook
- Zune`s Next Steps
- Safari Books Online Review
- Does Microsoft Get Touch Screens Now?
- Microsoft`s Record Quarterly Earnings Not En...
- Basic Operations and Registers in Assembly
- Assembly Coding within Visual C/C++ IDE
- New Microsoft Office Coming with a Twist
- Microsoft`s FUSE Labs Unveils Spindex Social...
- HP Slate with Windows 7: Dead or Alive?
- Windows Phone 7 Mobile OS to Rival Android a...

ASP Web Hosting ASP.Net Web Hosting Windows Web Hosting
ASP Free Forums 
 RSS  Tutorials RSS
 RSS  Forums RSS
 RSS  All Feeds
Site Map 
Request Media Kit
Write For Us Get Paid 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Privacy Policy 
Support 


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 11 - Follow our Sitemap
Most Popular Topics
All ASP.Net Tutorials