Home Programming Mastering LOB Development for Silverlight 5: A Case Study in Action

Mastering LOB Development for Silverlight 5: A Case Study in Action

books-svg-icon Book
eBook $39.99 $27.98
Print $65.99
Subscription $15.99 $10 p/m for three months
$10 p/m for first 3 months. $15.99 p/m after that. Cancel Anytime!
What do you get with a Packt Subscription?
This book & 7000+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with a Packt Subscription?
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with eBook + Subscription?
Download this book in EPUB and PDF formats, plus a monthly download credit
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with a Packt Subscription?
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with eBook?
Download this book in EPUB and PDF formats
Access this title in our online reader
DRM FREE - Read whenever, wherever and however you want
Online reader with customised display settings for better reading experience
What do you get with video?
Download this video in MP4 format
Access this title in our online reader
DRM FREE - Watch whenever, wherever and however you want
Online reader with customised display settings for better learning experience
What do you get with video?
Stream this video
Access this title in our online reader
DRM FREE - Watch whenever, wherever and however you want
Online reader with customised display settings for better learning experience
What do you get with Audiobook?
Download a zip folder consisting of audio files (in MP3 Format) along with supplementary PDF
What do you get with Exam Trainer?
Flashcards, Mock exams, Exam Tips, Practice Questions
Access these resources with our interactive certification platform
Mobile compatible-Practice whenever, wherever, however you want
BUY NOW $10 p/m for first 3 months. $15.99 p/m after that. Cancel Anytime!
eBook $39.99 $27.98
Print $65.99
Subscription $15.99 $10 p/m for three months
What do you get with a Packt Subscription?
This book & 7000+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with a Packt Subscription?
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with eBook + Subscription?
Download this book in EPUB and PDF formats, plus a monthly download credit
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with a Packt Subscription?
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with eBook?
Download this book in EPUB and PDF formats
Access this title in our online reader
DRM FREE - Read whenever, wherever and however you want
Online reader with customised display settings for better reading experience
What do you get with video?
Download this video in MP4 format
Access this title in our online reader
DRM FREE - Watch whenever, wherever and however you want
Online reader with customised display settings for better learning experience
What do you get with video?
Stream this video
Access this title in our online reader
DRM FREE - Watch whenever, wherever and however you want
Online reader with customised display settings for better learning experience
What do you get with Audiobook?
Download a zip folder consisting of audio files (in MP3 Format) along with supplementary PDF
What do you get with Exam Trainer?
Flashcards, Mock exams, Exam Tips, Practice Questions
Access these resources with our interactive certification platform
Mobile compatible-Practice whenever, wherever, however you want
  1. Free Chapter
    Express Introduction to Silverlight
About this book
Microsoft Silverlight is fully established as a powerful tool for creating and delivering Rich Internet Applications and media experiences on the Web. This book will help you dive straight into utilizing Silverlight 5, which now more than ever is a top choice in the Enterprise for building Business Applications. "Mastering LOB Development for Silverlight 5: A Case Study in Action" focuses on the development of a complete Silverlight 5 LOB application, helping you to take advantage of the powerful features available along with expert advice. Fully focused on LOB development, this expert guide takes you from the beginning of designing and implementing a Silverlight 5 LOB application, all the way through to completion. Accompanied by a gradually built upon case study, you will learn about data access via RIA and Web services, architecture with MEF and MVVM applied to LOB development, testing and error control, and much more.With "Mastering LOB Development for Silverlight 5: A Case Study in Action" in hand, you will be fully equipped to expertly develop your own Silverlight Line of Business application, without dwelling on the basics of Enterprise Silverlight development.
Publication date:
February 2012
Publisher
Packt
Pages
430
ISBN
9781849683548

 

Chapter 1. Express Introduction to Silverlight

Nowadays, starting a web development poses a considerable challenge, since clients have got used to having powerful desktop-based interfaces at their disposal, which can also be delivered in record time. If we focus on Line of Business (LOB) applications, we find the additional challenge which is the fact that our apps have to be ready for massive changes, taking into account tight deadlines without sacrificing stability. All of us have suffered that "little last-minute change". Everybody has heard things like, "We got to change the way in which discounts for purchases are managed. This could be ready in just five minutes, couldn't it?"

To overcome such situations, web developers can make use of a combination of ASP.NET (webforms or MVC), HTML, JavaScript, AJAX, and the more advanced HTML 5 and jQuery.

Nevertheless, when we implement LOB applications we often find that:

  • We have to struggle in order to make our pages consistent in different browsers; even in different versions of the same browser.

  • Our developers have to learn a language to develop client side, and another one to develop the server side.

  • JavaScript is a polemic language—love it or hate it. For some developers it is not object-oriented (although it has OO capabilities) and is an interpreted language. One only has to forget to add a semicolon, or introduce a syntax error when typing a command, and our application may produce an execution time error.

  • HTML 5 only works in updated browsers. Could you imagine yourself telling your client something like, "Well, what you have to do is install the latest version of Chrome or IE on your 1,000 PCs. This is also applicable to your associate companies."

  • We have to mix business and presentation logic. We try to avoid going to the server, for instance, to make validations which do not require reading a database. That is to say, we mix the reading of an input or an HTML ComboBox with the realization of validations. For example, if the user chooses more than four high-end products and is a premium client, we can enable a special 10 percent discount. This causes serious trouble when changes are required in the page layout, even if they are insignificant.

 

Introduction to Silverlight


Microsoft has published a plugin called Silverlight (the word plugin reminds us of Flash, one of the most accepted plugin-based technologies) which allows us to encode with sturdy, compiled languages (such as C# and VB.NET). This plugin incorporates a lite version, that is the .NET Framework, which offers us the possibility to take advantage of everything offered at the client side while implementing a new markup language called XAML. The advantages of using Silverlight are as follows:

  • Our applications are sturdier; for example, allowing us to implement automatic unit testing at the client side.

  • We can decouple business presentation and implement an architecture at the client side.

  • We can decouple roles. While a designer can deal with presentation, we as developers are able to focus on the business of building the application.

  • Our application is more scalable (we free up resources on the server) and we do not depend on tricks to maintain application status.

  • We can have a standard XAML implemented the same way in every single browser. No more headaches such as, "it looks good in IE6 but not in IE7, or Firefox, and so on".

In addition, Silverlight is multi-platform (for example, Windows or Mac) and multi-device (computers, mobile devices with Symbian or WP7 support, for instance, among others).

 

Installation


In this book, we are going to deal with Silverlight 5. The tool that Microsoft recommends for development is Visual Studio 2010. Therefore, we will have to install the following software:

All of these individual links are available at http://www.silverlight.NET/getstarted/.

Note

In case you cannot install Visual Studio 2010, the 2008 version of the product will provide you with an interaction limited to Silverlight 3, hence you will not be able to follow all of the contents of this book.

 

Silverlight architecture


Silverlight is a plugin installed in web browsers in a quick and clean way similar to Flash. That is, Flash is in a controlled environment, and applications run under a sandbox environment.

As for the architecture, if we compare it to a standard web application, we can substitute XAML for HTML, C# or VB.NET for JavaScript, and so on. We can also make use of a reduced version of the.NET Framework as shown in the following figure:

The plugin is about 4 MB in size and does not depend on the desktop version of Microsoft .NET Framework.

All this is fine but there a few questions to be answered such as how is the Silverlight application installed on a server? How is it executed on a client machine? Let's see how it works:

  • When we build a Silverlight project, an XAP file is generated (Silverlight can also be configured to generate several files).

  • This file is just a ZIP file with all the necessary assemblies and resources to execute the application.

  • This XAP file is stored on our web server.

  • In an HTML page, we reference it using an OBJECT tag.

  • When the user navigates to that page, the XAP file of the application is downloaded and unzipped. Then, the application starting point is sought and the application is executed.

Application execution takes place in a controlled sandbox environment, so a malicious developer cannot format the client's HDD (in some cases, user confirmation is required to interact with the hardware, otherwise interaction is simply denied to the application).

To get a better idea of how it works, let's compare an HTML web application with a Silverlight one:

Creating the Hello World project

Now that we have our development environment ready, we will create our first Silverlight project. Of course, it will be the classic Hello World. On this occasion, we will use the example to learn:

  • How to create a new project

  • How to encode directly into the markup language

  • How to drag elements from the Toolbox palette and configure them using Properties

  • How to respond to an event and call a Code-Behind method

Creating a new project

Let's open Visual Studio and start creating the app:

  1. To create a new project, we must launch Visual Studio. Select File | New Project and choose the Silverlight Application option:

  2. Once the route and type of project has been chosen, a dialog will appear, asking us if we want to create a web project to host our Silverlight application.

  3. Click on OK (a Silverlight application needs a web page which instantiates it with an OBJECT tag).

    We now have the solution created. It consists of two projects:

    • Silverlight project: The wizard creates an entry point (.app file) and a default page (MainPage). In the default MainPage, we can see the layout definition (MainPage.xaml) and its associated Code-Behind (mainpage.cs). It is advisable to remember that this Code-Behind is executed at the client side and not at the server side.

    • Web project: This simply consists of an ASP.NET page and an HTML page to try our Silverlight application.

      We can clearly see every element of the solution in the following figure:

Coding directly into the markup language

In this section, we will add our first Silverlight controls, encoding them directly into the markup language. We will see next how to carry out the same operation via drag-and-drop:

  1. Open the file MainPage.xaml.

  2. Add a text block containing the expression Hello World. To do this, we will enter the following code:

    <Grid x:Name="LayoutRoot" Background="White">
      <TextBlock Text="Hello World!" FontSize="20"/>
    </Grid>

    Tip

    Downloading the example code

    You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

  3. We must build the project and execute the example by clicking on the play icon.

  4. We can now see our first Silverlight application in action!

 

Dragging-and-dropping controls


It is highly recommended that you have a good knowledge of the XAML markup language and some experience practicing it. Nevertheless, if you intend to make a rapid prototype or just need to begin developing immediately, you can use the drag-and-drop controls from the toolbar directly in to the form (as in a WinForms application).

You can also edit the features of a control by accessing the Properties window as shown in the following screenshot:

In order to achieve a higher control over the windows layout (with the aim to, for example, achieve an optimal disposition of the controls in different resolutions), we suggest learning the XAML markup language or using Expression Blend, which is a tool for designers (and developers).

 

Interacting with Code-Behind


In the following example, we will allow the user to change the message Hello World! to one of their choice. Starting from the previous example, we will follow the given steps:

  1. Open the mainpage.xaml file.

  2. Replace the XAML code inserted in the previous example by the one highlighted as follows (we have added an additional textbox, a button, and identifiers for the controls).

    <Grid x:Name="LayoutRoot" Background="White">
      <StackPanel Orientation="Vertical">
      <TextBlock x:Name="tbLabel" 
      Text="Hello World!" 
      FontSize="20"/>
                <StackPanel Orientation="Horizontal">
                    <TextBlock 
                        Text="New Text:" 
                        FontSize="16"/>
                    <TextBox 
                        x:Name="txInput" 
                        Width="120"/>
                    <Button Content="Change"/>
                </StackPanel>
            </StackPanel>
        </Grid>
  3. When we build and execute the project, we realize that our window now has the aspect as shown in the following screenshot:

  4. Next, we must implement the response to the Click event of the Change button.

  5. Hook to the Click event directly in the XAML file. As soon as we start typing, IntelliSense (Microsoft's implementation of autocompletion) will ask us if we want to create the method (hitting Enter or Tab would create the method with the default name or with the name of the control after selecting a Click event).

  6. Execute the same operation from the Properties panel (or by directly double-clicking on the button control):

  7. As a result, XAML will look as follows:

    <Button Content="Change" Click="Button_Click"/>
  8. In Code-Behind, in the method invoked by the Click event, we must add a line of code, which transfers the text content entered by the user to the tag where we showed 'Hello World'.

    private void Button_Click(object sender, RoutedEventArgs e)
    {
      tbLabel.Text = txInput.Text;
    }
  9. When we execute, we will be able to enter a new text that substitutes 'Hello World'.

 

XAML basic concepts


Now that we have taken our first steps with Silverlight, let's have a quick introduction to some basic concepts in XAML.

What is XAML?

Extensible Application Markup Language (XAML) is a declarative language. Specifically, XAML can initialize objects and set properties of objects, using a language structure that shows hierarchical relationships between multiple objects, and uses a backing type convention that supports extension of types. You can create visible user interface (UI) elements in the declarative XAML markup. You can then use a separate Code-Behind file to respond to events and manipulate the objects you declare in XAML (For more information on XAML, you can take a look at http://msdn.microsoft.com/en-us/library/cc189036(v=vs.95).aspx).

The advantages of XAML when compared to HTML are as follows:

  • XAML is a modern language, adapted to the current needs of users and implemented from scratch (whereas HTML suffers from organic growth)

  • We have only one way to implement it, which avoids us headaches derived from the problems of adaptation with different browsers and their versions

  • There is a clear differentiation between declarative (XAML) and business logic/code parts (.cs Code-Behind)

The best you can do is try it and see for yourself.

Mastering LOB Development for Silverlight 5: A Case Study in Action
Unlock this book and the full library FREE for 7 days
Start now