Microsoft Silverlight 4 Data and Services Cookbook - Cleeren Gill - E-Book

Microsoft Silverlight 4 Data and Services Cookbook E-Book

Cleeren Gill

0,0
47,99 €

-100%
Sammeln Sie Punkte in unserem Gutscheinprogramm und kaufen Sie E-Books und Hörbücher mit bis zu 100% Rabatt.

Mehr erfahren.
Beschreibung

Microsoft Silverlight is a programmable web browser plugin that enables features including animation, vector graphics, and audio-video playback – features that characterize Rich Internet Applications. However, Silverlight is a great (and growing) Line-Of-Business platform and is increasingly being used to build data-driven business applications. Silverlight Data Services enable efficient access to your data, allowing you to draw on multiple sources of data and solve particular data problems. There is very little existing material that demonstrates how to build data-driven solutions with the platform. Silverlight 3 made a big step into Line-Of-Business data services and Silverlight 4 builds further upon this. This book will enable .NET developers to get their finger on the pulse of data-driven business applications in Silverlight.This book is not a general Silverlight 3/4 overview book; it is uniquely aimed at developers who want to build data-driven applications. It focuses on showing .NET developers how to interact with, and handle multiple sources of data in Silverlight business applications, and how to solve particular data problems following a practical hands-on approach, using real-world recipes. It is a practical cookbook that teaches you how to build data-rich business applications with Silverlight that draw on multiple sources of data. Most of the covered features work both in Silverlight 3 and 4. However, we cover some that are specific for Silverlight 4, which will therefore not work with Silverlight 3. Where this is the case, it is clearly indicated.Packed with reusable, real-world recipes, the book begins by introducing you to general principles when programming Silverlight. It then dives deep into the world of data services, covering all the options available to access data and communicate with services to get the most out of data in your Silverlight business applications, whilst at the same time providing a rich user experience. Chapters cover data binding, data controls, the concepts of talking to services, communicating with WCF, ASMX and REST services, and much more.By following the practical recipes in this book, which are of varying difficulty levels, you will learn the concepts needed to create data-rich business applications—from the creation of a Silverlight application, to displaying data in the Silverlight application and upgrading your existing applications to use Silverlight. Each recipe covers a data services topic, going from the description of the problem, through a conceptual solution to a solution containing sample code.

Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:

EPUB

Seitenzahl: 546

Veröffentlichungsjahr: 2010

Bewertungen
0,0
0
0
0
0
0
Mehr Informationen
Mehr Informationen
Legimi prüft nicht, ob Rezensionen von Nutzern stammen, die den betreffenden Titel tatsächlich gekauft oder gelesen/gehört haben. Wir entfernen aber gefälschte Rezensionen.



Table of Contents

Silverlight 4 Data and Services Cookbook
Credits
About the authors
About the reviewers
Preface
What this book covers
What you need for this book
Who this book is for
Conventions
Reader feedback
Customer support
Downloading the example code for the book
Errata
Piracy
Questions
1. Learning the Nuts and Bolts of Silverlight 4
Introduction
Getting our environment ready to start developing Silverlight applications
How to do it...
How it works...
See also
Creating our first service-enabled and data-driven Silverlight 4 application using Visual Studio 2010
Getting ready
How to do it...
How it works...
The solution and project structure
Services
Data binding
See also
Using the workflow between Visual Studio 2010 and Blend 4
Getting ready
How to do it...
How it works...
See also
Using source control in Visual Studio 2010 and Blend 4
Getting ready
How to do it...
How it works...
There's more...
Commonly used terms in TFS
Deploying a Silverlight application on the server
How to do it...
How it works...
Configuration changes on the server
What if the server doesn't allow using XAP?
2. An Introduction to Data Binding
Introduction
Displaying data in Silverlight applications
Getting ready
How to do it...
How it works...
There's more...
See also
Creating dynamic bindings
Getting ready
How to do it...
How it works...
There's more...
Binding data to another UI element
Getting ready
How to do it...
How it works...
There's more...
Element bindings without bindings
See also
Binding collections to UI elements
Getting ready
How to do it...
How it works...
There's more...
See also
Enabling a Silverlight application to automatically update its UI
Getting ready
How to do it...
How it works...
Single objects
Collections
Cleaning up the code
Obtaining data from any UI element it is bound to
Getting ready
How to do it...
How it works...
See also
Using the different modes of data binding to allow persisting data
Getting ready
How to do it...
How it works...
There's more...
See also
Data binding from Expression Blend 4
How to do it...
How it works...
Using Expression Blend 4 for sample data generation
How to do it...
3. Advanced Data Binding
Introduction
Hooking into the data binding process
Getting ready
How to do it...
How it works...
The ConvertParameter
Displaying images based on a URL with converters
Replacing converters with Silverlight 4 BindingBase properties
Getting ready
How to do it...
How it works...
See also
Validating databound input
Getting ready
How to do it...
How it works...
There's more...
See also
Validating data input using attributes
Getting ready
How to do it...
How it works...
There's more...
Other uses of data annotations
See also
Validating using IDataErrorInfo and INotifyDataErrorInfo
Getting ready
How to do it...
How it works...
There's more...
See also
Using templates to customize the way data is shown by controls
Getting ready
How to do it...
How it works...
Building a change-aware collection type
Getting ready
How to do it...
How it works...
See also
Combining converters, data binding, and DataContext into a custom DataTemplate
Getting ready
How to do it...
How it works...
See also
4. The Data Grid
Introduction
Displaying data in a customized DataGrid
Getting ready
How to do it...
How it works...
There's more
See also
Inserting, updating, and deleting data in a DataGrid
Getting ready
How to do it...
How it works...
See also
Sorting and grouping data in a DataGrid
Getting ready
How to do it...
How it works...
There's more...
Sorting a template column
See also
Filtering and paging data in a DataGrid
Getting ready
How to do it...
How it works...
There's more...
See also
Using custom columns in the DataGrid
Getting ready
How to do it...
How it works...
There's more...
Implementing master-detail in the DataGrid
Getting ready
How to do it...
How it works...
There's more...
Validating the DataGrid
Getting ready
How to do it...
How it works...
There's more...
RIA Services and data annotations
See also
5. The DataForm
Introduction
Displaying and editing an object using the DataForm
Getting ready
How to do it...
How it works...
There's more...
More on DataForm behavior
Validating a DataForm or a DataGrid
Managing the editing of an object on different levels
See also
Displaying and editing a collection using the DataForm
Getting ready
How to do it...
How it works...
There's more...
See also
Customizing the DataForm using Data Annotations
Getting ready
How to do it...
How it works...
There's more...
Using Data Annotations for validation
See also
Customizing the DataForm using a custom DataTemplate
Getting ready
How to do it...
How it works...
There's more...
See also
Customizing the DataForm template
Getting ready
How to do it...
How it works...
There's more...
Getting the default template of a control
See also
Validating the DataForm
Getting ready
How to do it...
How it works...
There's more...
Other uses of Data Annotations
RIA Services and Data Annotations
See also
6. Talking to Services
Introduction
Connecting and reading from a standardized service
Getting ready
How to do it...
How it works...
See also
Persisting data using a standardized service
Getting ready
How to do it...
How it works...
See also
Configuring cross-domain calls
Getting ready
How to do it...
How it works...
Working cross-domain from a trusted Silverlight application
Getting ready
How to do it...
How it works...
See also
Reading XML using HttpWebRequest
Getting ready
How to do it...
How it works...
The request
The response
Threading headaches
See also
Reading out an RSS feed
Getting ready
How to do it...
How it works...
There's more...
RSS versions
See also
Aggregating RSS feeds
Getting ready
How to do it...
How it works...
There's more...
See also
Talking with services in the Windows Azure cloud
Getting ready
How to do it...
How it works...
There's more...
What about other cloudservices?
See also
Persisting data to and reading data from the Windows Azure cloud
Getting ready
How to do it...
How it works...
There's more...
What about relational data storage?
See also
Using socket communication in Silverlight
Getting ready
How to do it...
How it works...
The policy server
The socket server
Connecting the Silverlight application
7. Talking to WCF and ASMX Services
Introduction
Invoking a service that exposes data
Getting ready
How to do it...
How it works...
The WCF service
Connecting to the service
See also
Invoking a service such as Bing.com
Getting ready
How to do it...
How it works...
There's more...
See also
Handling faults when calling a service
Getting ready
How to do it...
How it works...
There's more...
Optimizing performance using binary XML
Getting ready
How to do it...
How it works...
The default configuration is different depending on the version of Silverlight you're using
Using binary encoding is not a security measure
See also
Using duplex communication
Getting ready
How to do it...
How it works...
The PollingDuplexBinding: polling, binding, and assemblies
The service
The client
See also
Using duplex communication with the WCF net.tcp binding
Getting ready
How to do it...
How it works...
Setup requirements
See also
Debugging a service in Silverlight
Getting ready
How to do it...
How it works...
Types of faults
Declared faults
Undeclared faults
Ensuring data is encrypted
Getting ready...
How to do it...
How it works...
See also
Securing service communication using message-based security
Getting ready
How to do it...
How it works...
Transport security is required
See also
Uploading files to a WCF service
Getting ready
How to do it...
How it works...
See also
Displaying images as a stream from a WCF service
Getting ready
How to do it...
How it works...
See also
Hooking into proxy creation using slsvcutil.exe
Getting ready
How to do it...
How it works...
Calling a WCF service from Silverlight using ChannelFactory
Getting ready
How to do it...
How it works...
See also
8. Talking to REST and WCF Data Services
Introduction
Reading data from a REST service
Getting ready
How to do it...
How it works...
See also
Parsing REST results with LINQ-To-XML
Getting ready
How to do it...
How it works...
See also
Persisting data using a REST service
Getting ready
How to do it...
How it works...
There's more...
See also
Working with the ClientHttpStack
Getting ready
How to do it...
How it works...
Advantages of ClientHttpStack
See also
Communicating with a REST service using JSON
Getting ready
How to do it...
How it works...
See also
Using WCF Data Services with Silverlight
Getting ready
How to do it...
How it works...
Locked-down services
See also
Reading data using WCF Data Services
Getting ready
How to do it...
How it works...
There's more...
See also
Persisting data using WCF Data Services
Getting ready
How to do it...
How it works...
There's more...
Talking to Flickr
Getting ready
How to do it...
How it works...
There's more...
Flickr... more information
See also
Talking to Twitter over REST
Getting ready
How to do it...
How it works...
See also
Passing credentials and cross-domain access to Twitter from a trusted Silverlight application
Getting ready
How to do it...
How it works...
Let's go cross-domain!
Pass me those credentials, will you?
See also
9. Talking to WCF RIA Services
Introduction
Setting up a data solution to work with RIA Services
Getting ready
How to do it...
How it works...
There's more...
The WCF RIA Services Toolkit
See also
Getting data on the client
Getting ready
How to do it...
How it works...
There's more...
See also
Sorting and filtering data
Getting ready
How to do it...
How it works...
See also
Persisting data to the server
Getting ready
How to do it...
How it works...
There's more...
See also
Persisting a Unit of Work/changeset
Getting ready
How to do it...
How it works...
There's more...
See also
Working with concurrency and transactions
Getting ready
How to do it...
How it works...
Tracking a user's identity
Getting ready
How to do it...
How it works...
There's more...
See also
Controlling a user's access to a service and service methods
Getting ready
How to do it...
How it works...
There's more...
Validating data using data annotations
Getting ready
How to do it...
How it works...
There's more...
See also
Validating data using shared code
Getting ready
How to do it...
How it works...
See also
Using the DomainDataSource
Getting ready
How to do it...
How it works...
There's more...
See also
Using the DomainDataSource to page through data
Getting ready
How to do it...
How it works...
See also
Sorting, filtering, and grouping data using the DomainDataSource
Getting ready
How to do it...
How it works...
There's more...
10. Converting Your Existing Applications to Use Silverlight
Introduction
Calling a stored procedure from Silverlight
Getting ready
How to do it...
How it works...
See also
Working with LINQ to SQL from Silverlight
Getting ready
How to do it...
How it works...
There's more...
What is LINQ TO SQL?
See also
Working with the Entity Framework from Silverlight
Getting ready
How to do it...
How it works...
There's more...
What is the Entity Framework?
See also
Converting a Silverlight application to use WCF RIA Services
Getting ready
How to do it...
How it works...
See also
Using ASP.NET Authentication in Silverlight
Getting ready
How to do it...
How it works...
See also
Using ASP.NET Roles in Silverlight
Getting ready
How to do it...
How it works...
See also
Using ASP.NET Profiles in Silverlight
Getting ready
How to do it...
How it works...
See also
A. Appendix
Creating a REST service from WCF
Installing a SQL Server database
Attaching an MDF file
Executing a query file
Working with Fiddler
Local traffic
Working with the Silverlight control toolkit
Index

Silverlight 4 Data and Services Cookbook

Gill Cleeren

Kevin Dockx

Silverlight 4 Data and Services Cookbook

Copyright © 2010 Packt Publishing

All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.

Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the authors, Packt Publishing, nor its dealers or distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book.

Packt Publishing has endeavored to provide trademark information about all the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.

First published: April 2010

Production Reference: 1200410

Published by Packt Publishing Ltd. 32 Lincoln Road Olton Birmingham, B27 6PA, UK.

ISBN 978-1-847199-84-3

www.packtpub.com

Cover Image by Sandeep (<[email protected]>)

Credits

Authors

Gill Cleeren

Kevin Dockx

Reviewers

Kevin DeRudder

Mike Hanley

Tarkan Karadayi

Kris Van Der Mast

Acquisition Editor

Kerry George

Development Editor

Neha Patwari

Technical Editors

Roger D'souza

Arani Roy

Copy Editor

Sneha Kulkarni

Indexer

Monica Ajmera Mehta

Editorial Team Leader

Mithun Sehgal

Project Team Leader

Priya Mukherji

Project Coordinator

Zainab Bagasrawala

Proofreader

Chris Smith

Aaron Nash

Production Coordinator

Melwyn D'sa

Cover Work

Melwyn D'sa

About the authors

Gill Cleeren is Microsoft's Regional Director, MVP ASP.NET, INETA speaker bureau member, and Silverlight Insider. He lives in Belgium, where he works as a .NET architect. Since its introduction, Gill has been a .NET enthusiast. For the first few years of his career, he developed ASP.NET applications. The very day that Silverlight was introduced to the world, back in 2007, he started working with the technology. Since then, he has given many training sessions and developed several applications, both with Silverlight and WPF.

As MVP and Regional Director, Gill is also very active in the Belgian and international community as a speaker and user group leader for Visug, the Belgian .NET user group. His blog on .NET, ASP.NET, and Silverlight can be found at www.snowball.be.

For me, writing a book about the things I like to do has always been a dream. For some time now, Silverlight has been my hobby and passion. I spend hours in front of my laptop, both during and after working hours, staring at some Silverlight project. When James Lumsden of Packt Publishing offered me the possibility to write a book on Silverlight, I was immediately carried away by the idea. Packt brought me in touch with Kevin Dockx, also from Belgium, who agreed on co-authoring this book. Days after that, we sat together and started working on the book you're holding in your hands now. During the writing period, the people of Packt have been most helpful in every way. I really want to thank Kerry George, Neha Patwari, and Zainab Bagasrawala for replying to the countless mails we have exchanged! I also want to thank the review team: Kevin DeRudder, Mike Hanley, Tarkan Karadayi, and Kris Van Der Mast. And of course, a very big thank you to both my mother and my lovely girlfriend Lindsey, for the patience during the days I spent more time writing than being with you.

Kevin Dockx lives in Belgium and works at RealDolmen, one of Belgium's biggest ICT companies, where he is a technical specialist/project leader on .NET web applications, mainly Silverlight, and a solution manager for Rich Applications (Silverlight, WPF, Surface).His main focus lies on all things Silverlight, but he still keeps an eye on the new developments concerning other products from the Microsoft .NET (Web) Stack. As a Silverlight enthusiast, he's a regular speaker on various national and international events, like Microsoft DevDays in The Netherlands or on BESUG events (the Belgian Silverlight User Group). His blog, which contains various tidbits on Silverlight, .NET, and the occasional rambling, can be found at http://blog.kevindockx.com.

I'd like to thank anyone who helped me out with this book, especially Maarten Balliauw, who pointed me in the right direction for the Windows Azure recipes. Besides that, I'd also like to thank my friends for sticking around while I was busy spending my time writing this book instead of socializing. :-)

About the reviewers

Kevin DeRudder is a .NET trainer and consultant at U2U, mainly focusing on the WEB. In the last years, Kevin was involved in lots of Silverlight and ASP.NET projects for a variety of clients and projects.

Mike Hanley is the VP of Engineering at Vertigo Software, Inc. (http://www.vertigo.com). He has worked as a Software Engineer, specializing on the Microsoft Platform for over 10 years. Recently, Mike has spent a great deal of time working on Silverlight applications for clients ranging from Hard Rock International to the Democratic National Committee. Most recently, Mike has worked on several live video events using IIS Smooth Streaming. Mike is a Certified Scrum Master and graduated from Harvey Mudd College with a B.S. in Computer Science. You can learn more about Mike on his blog: http://www.michaelhanley.org. During his spare time, Mike enjoys spending time with his wife Tylar and his first son Max. Mike also has an identical twin brother Kevin.

Tarkan Karadayi has been writing code since the age of 14. He has a Masters in Computer Science and is currently working as a Lead Developer.

I would like to thank my wife Anna, my parents, and my three sons Taran, Kyle, and Ryan for their love and support.

Kris van der Mast, an active and dedicated moderator at the official Microsoft ASP.NET forums, has reserved himself a place among the Most Valuable Professionals since 2007 and in 2009 earned the ASP Insider title. He's also a known member of several of the Belgian user groups.

Kris works for Ordina Belgium, a consultancy company working for large corporates in Belgium and abroad, as a senior .NET developer and architect. He also provides courses to clients in his specialization and technical interests, being web technologies.

Preface

This practical cookbook teaches you how to build data-rich business applications with Silverlight that draw on multiple sources of data. Most of the features covered work both in Silverlight 3 and 4. However, we cover some that are specific to Silverlight 4, which will therefore not work with Silverlight 3. Where this is the case, it will be clearly indicated.

Packed with reusable, real-world recipes, the book begins by introducing you to general principles when programming Silverlight. It then dives deep into the world of data services, covering all the options available to access data and communicate with services to make the most out of data in your Silverlight business applications, whilst at the same time providing a rich user experience. Chapters cover data binding, data controls, concepts of talking to services, communicating with WCF, ASMX, and REST services and much more.

By following the practical recipes in this book, which are of varying difficulty levels, you will learn concepts for creating data-rich business applications—from the creation of a Silverlight application, to displaying data in the Silverlight application and upgrading your existing applications to use Silverlight. Each recipe will cover a data services topic, going from the description of the problem, over a conceptual solution and a solution containing sample code.

What this book covers

Chapter 1, Learning the Nuts and Bolts of Silverlight 4 will get you up and running with Silverlight. While this book is aimed at developers who already have a basic knowledge of Silverlight, this chapter can act as a refresher. We'll also look at getting your environment correctly set up so that you enjoy developing Silverlight applications.

Chapter 2, An Introduction to Data Binding will explore how data binding works. We'll start by building a small data-driven application that contains the most important data binding features, to get a grasp of the general concepts. We'll also see that data binding isn't tied to just binding single objects to an interface; binding an entire collection of objects is supported as well. We'll also be looking at the binding modes. They allow us to specify how the data will flow (from source to target, target to source, or both). We'll finish this chapter by looking at the support that Blend 4 provides to build applications that use data binding features. In the next chapter, we'll be looking at the more advanced concepts of data binding.

Chapter 3, Advanced Data Binding teaches you advanced data binding concepts that can be used for customization, validations, and applying templates to data bound controls. We also have a look at converters, that are obvious hooks in data binding and their possibilities.

Chapter 4, The Data Grid covers recipes on how to work with the DataGrid. This is an essential control for applications that rely on (collections of) data.

Chapter 5, The DataForm covers recipes on how to work with the DataForm, which is an essential control for the applications that rely on (collections of) data.

Chapter 6, Talking to Services talks about the rich set of options that Silverlight provides to communicate with services.

Chapter 7, Talking to WCF and ASMX Services discovers Silverlights' built-in support for communicating with Windows Communication Foundation (WCF) and classic ASMX web services. Perform unidirectional as well as bidirectional communication with much better performance using net.tcp binding in WCF using the recipes in this chapter.

Chapter 8, Talking to REST and ADO.NET Data Services takes advantage of REST, which can be significant in the case of Silverlight. We will also look at how we can work with ADO.NET Data Services. You will abstract away a lot of plumbing code with the use of the client-side library that is available for use with Silverlight.

Chapter 9, Talking to WCF RIA Services discovers a new, free framework developed by Microsoft-WCF RIA Services- to simplify Line of Business RIA development. This chapter will teach you to easily design and develop rich data-driven applications by simplifying the access to the validation of and the authentication of services, service methods, and entities.

Chapter 10, Converting Your Existing Applications to Use Silverlight, integrates Silverlight into existing web applications and performs a step-by-step migration where Silverlight starts to become part of a system. This chapter is all about techniques to leverage your existing knowledge, applications, and business requirements to Silverlight. This chapter contains a recipe that shows how to enable WCF RIA Services for existing applications.

Appendix talks about creating a REST service from WCF, installing a SQL Server database, and working with Fiddler and Silverlight control toolkit

What you need for this book

To work with the recipes in this book, it's required that you have Visual Studio installed. This book targets Silverlight 4, which works only with Visual Studio 2010. Many of the recipes in the book will also work in Silverlight 3, so for these recipes, you have the choice of Visual Studio 2008 or 2010. We do recommend using Visual Studio 2010, as it features a lot of enhancements for developing with Silverlight. In both cases, you'll of course need to install the Silverlight Tools, which will update your Visual Studio instance to work with Silverlight. Some recipes also require Blend 4 to be installed on your machine (again, if working with Silverlight 3, Blend 3 will suffice here as well).

The first recipe of Chapter 1, Getting our environment ready to start developing Silverlight applications, explains in detail how to get these tools and how to install them.

Who this book is for

If you are a .NET developer who wants to build professional data-driven applications with Silverlight, then this book is for you. Basic Silverlight experience and familiarity with accessing data using ADO.NET in regular .NET applications is required.

Conventions

In this book, you will find a number of styles of text that distinguish between different kinds of information. Here are some examples of these styles, and an explanation of their meaning.

Code words in text are shown as follows: "Finally, the DataReader and connection are closed and the StoreDTO object is returned."

A block of code will be set as follows:

<TextBlock x:Name="AmountTextBlock" Text="{Binding ElementName=AmountSlider, Path=Value}"> </TextBlock>

New terms and important words are shown in bold. Words that you see on the screen, in menus or dialog boxes for example, appear in our text like this: "Do this by right-clicking, selecting Add New Item, and then selecting LINQ TO SQL Classes.".

Note

Warnings or important notes appear in a box like this.

Tip

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.

To send us general feedback, simply drop an email to <[email protected]>, and mention the book title in the subject of your message.

If there is a book that you need and would like to see us publish, please send us a note in the SUGGEST A TITLE form on www.packtpub.com or email to <[email protected]>.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code for the book

Visit http://www.packtpub.com/files/code/9843_Code.zip to directly download the example code.

The downloadable files contain instructions on how to use them.

Errata

Although we have taken every care to ensure the accuracy of our contents, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in text or code—we would be grateful if you would report this to us. By doing so, you can save other readers from frustration, and help us to improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/support, selecting your book, clicking on the let us know link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded on our website, or added to any list of existing errata, under the Errata section of that title. Any existing errata can be viewed by selecting your title from http://www.packtpub.com/support.

Piracy

Piracy of copyright material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at <[email protected]> with a link to the suspected pirated material.

We appreciate your help in protecting our authors, and our ability to bring you valuable content.

Questions

You can contact us at <[email protected]> if you are having a problem with any aspect of the book, and we will do our best to address it.

Chapter 1. Learning the Nuts and Bolts of Silverlight 4

In this chapter, we will cover the following topics:

Getting our environment ready to start developing Silverlight applicationsCreating our first service-enabled and data-driven Silverlight 4 application using Visual Studio 2010Using the workflow between Visual Studio 2010 and Blend 4Using source control in Visual Studio 2010 and Blend 4Deploying a Silverlight application on the server

Introduction

While we assume some basic knowledge of Silverlight for this book, we also know that developers have very little time to grasp all the new technologies that keep coming out. Therefore, this first chapter contains all that we need to know to get going with Silverlight. We'll also guide you through the required tools and installations for a perfect Silverlight development environment.

Silverlight was released in the first half of 2007, and since then, it has created a lot of buzz. While ASP.NET is a server-side development platform, with the arrival of Silverlight, the focus has shifted to the client side again. A Silverlight application runs in the browser of the client and on a specific version of the Common Language Runtime (CLR).

A big benefit for developers is that Silverlight uses .NET from version 2 onwards. It has a trimmed-down version of the Base Class Library (BCL) that is impressively extended considering the size of the Silverlight plugin (less than 5 MB). Because of the similarities, many skills achieved from developing applications in the full .NET framework can be leveraged for the creation of Silverlight applications.

Silverlight itself can be considered as a trimmed-down version of its desktop counterpart, Windows Presentation Foundation (WPF). Between Silverlight 4 and WPF 4, there are still some differences. Some features are included in WPF 4, but aren't in Silverlight 4 and vice versa. It's possible to reuse code written for one technology in the other. However, upfront planning is required to ensure a smooth transition between the two technologies. Microsoft has released a whitepaper based on this aspect that provides more information on how to write applications that target both Silverlight and WPF. This document can be found at http://wpfslguidance.codeplex.com.

With the release of Silverlight 2, Microsoft made it clear that Silverlight is aimed at both creating rich and interactive applications, and next-level enterprise applications in the browser. The latter can be easily seen with the addition of a rich control set, support for many types of services and platform features such as data binding.

Due to its client-side characteristics, Silverlight applications need to perform particular tasks to get data. It doesn't support client-side databases—not even in version 4. The way to retrieve data is through services. Silverlight 3 brought some interesting features to the platform in this area such as support for binary XML, the WCF RIA services, and simplified duplex service communication. Silverlight 4 continued in the same manner, with improvements in data binding, support for net.tcp communication, cross-domain access to services by means of Trusted Silverlight applications, and much more. All these added features are a proof of the commitment Microsoft is making to position Silverlight as a platform for building enterprise applications.

In this chapter, we'll get you up and running with Silverlight. While this book is aimed at developers who already have a basic knowledge of Silverlight, this chapter can act as a refresher. We'll also look at getting your environment correctly set up so that you enjoy developing Silverlight applications.

Getting our environment ready to start developing Silverlight applications

In this recipe, we'll look at what we need to install to start developing Silverlight applications. We'll learn about the basic tools that we need as a developer and also take a look at the designer tools that can come in handy for developers as well.

How to do it...

To start developing Silverlight applications, we'll need to install the necessary tools and SDKs. Carry out the following steps in order to get started:

We need to make sure we install Visual Web Developer Express 2010 (available for free at http://www.microsoft.com/express/downloads/) or Visual Studio 2010 (trial version available at http://www.microsoft.com/visualstudio/en-us/download).Go to http://www.silverlight.net/getstarted/ to download and install the Silverlight 4 Tools for Visual Studio 2010. Open Visual Studio 2010 after installation. Visual Studio 2010 ships with Silverlight 3 templates installed out of the box, the tools add support for version 4.Go to http://www.microsoft.com/expression/ to download and install Blend 4.

How it works...

For Silverlight development, the minimum that we need are the developer tools. These will integrate with Visual Studio 2008 (if you're using Silverlight 3) or 2010. In Visual Studio 2010, a nice, visual designer is added for editing our XAML code. In the 2008 version, this designer doesn't exist. When installing the developer tools for Silverlight 4, the following components are automatically downloaded and installed:

Silverlight 4 developer runtimeSilverlight 4 software development kit and Visual Studio project supportWCF RIA services

We can write XAML code using Visual Studio. However, if you're serious about designing, you might want to consider using Microsoft Expression Blend. This tool, primarily aimed at designers, should be seen as an application that generates XAML for us by means of a rich number of options and an easy-to-use interface. It also integrates nicely with Visual Studio and source control software integration is available as well.

See also

After having installed all the necessary tools, it might be worth taking a look at the Creating our first service-enabled and data-driven Silverlight 4 application using Visual Studio 2010 recipe as well as the Using the workflow between Visual Studio 2010 and Blend 4 recipe. In these recipes, we create an entire application in Visual Studio 2010 and Blend 4 respectively.

Using source control in Visual Studio 2010 and Blend 4

When working on slightly larger projects in teams, source control is an absolute necessity. By far, the most popular source control system in the Microsoft world today is Team Foundation Server (TFS). This recipe explains all that we need to get TFS to work with Silverlight applications in Visual Studio and Blend. It doesn't explain how to work with TFS itself.

Getting ready

Before getting started, make sure that you have installed Blend 4 and the necessary developer tools as described in the Getting our environment ready to start developing Silverlight applications section.

How to do it...

To start using TFS as a versioning system with a Silverlight-enabled solution, we need to perform the following steps:

Using TFS source control with a Silverlight application in Visual Studio is exactly the same as using it with any other type of Visual Studio project. Team Explorer is used for this purpose and this is automatically installed out of the box with Visual Studio 2010. (For Visual Studio 2008, it has to be separately installed from http://www.microsoft.com/downloads/details.aspx? FamilyID=0ed12659-3d41-4420-bbb0-a46e51bfca86&DisplayLang=en.)Blend 4 also supports source control out of the box. However, for Blend 3, TFS update KB967483 must be downloaded and installed from http://code.msdn.microsoft.com/KB967483. This update enables TFS source control for Expression Blend 3.Once these are installed, we can use TFS source control from within Blend for any solution bound to the TFS source control. Right-click on any file in the Solution Explorer window to view the source control options just as we would do in Visual Studio's Solution Explorer window. We can now check out, check in, and merge files from Expression Blend.

How it works...

Team Foundation Server source control is the preferred way of enabling version control on our projects. Explaining in detail how to work with TFS is beyond the scope of this book, but the following are a few basic steps and references:

We must make sure that we have the correct permissions on our Team Foundation Server to handle the tasks we need to do. We need different permissions to (for example) create projects than to check out files. Have a look at this MSDN article to learn how to set these permissions: http://msdn.microsoft.com/en-us/library/ms252587.aspx.Next, we'll need to connect to TFS using Team Explorer. Have a look at this MSDN article to learn how to do that: http://msdn.microsoft.com/en-us/library/ms181474.aspx.We'll now need to create a workspace on our machine. We can look at the workspace as a local folder containing copies of the source-controlled files on the TFS. For more information, have a look at this MSDN article: http://msdn.microsoft.com/en-us/library/ms181384.aspx.After we've created a local workspace, we can download the files from the TFS to that local folder. More information about this can be found at http://msdn.microsoft.com/en-us/library/ms181385.aspx.We're now able to open our source-controlled solution in Blend and/or Visual Studio and check out files, merge files, add projects, and so on depending on the permissions.

There's more...

While working a lot with TFS, an interesting feature to download is the Team Foundation Server Power Tools. This is a set of extra features that is added to TFS and is mainly aimed at power users. It can be downloaded at http://msdn.microsoft.com/en-us/teamsystem/bb980963.aspx.

Commonly used terms in TFS

A complete glossary of TFS terms can be found at http://msdn.microsoft.com/en-us/library/ms242882.aspx. As a reference, the following are a few of the more commonly used ones along with their brief explanation:

Term

Explanation

TFS workspace

A TFS workspace is a location on the Team Foundation Server (TFS) where a record of changes between local files and corresponding repository files is stored. It can also be thought of as a copy of the client-side directory, a staging ground where local changes are persisted until they are checked into the server, or a collection of working folder mappings that can be viewed and edited.

Working folder

A working folder should be seen as a client-side representation of the TFS workspace. Binding the TFS workspace to the client-side working folder is done through a TFS workspace mapping.

Check in

Check in refers to the task of committing a pending change/pending changes to a TFS repository. When you check in pending changes, a new changeset is created on the server.

Check out

Check out refers to the task of notifying the TFS server that you are changing the status of a resource from locked to writeable. When you check out for edit, TFS appends an edit to that resource.

Get latest

Get latest refers to the task of retrieving the most recent version of a file from the TFS source control to your local working folder.

Deploying a Silverlight application on the server

Once we have a Silverlight application ready, we will want to show it to the rest of the world. This means deploying it!

While Silverlight is a .NET technology, it doesn't require .NET to be installed on the server. Remember that it's a client-side technology. The Silverlight plugin on the client will download and run the application using the version of the Common Language Runtime (CLR) embedded in the Silverlight plugin. In this recipe, we'll look at how we can deploy a Silverlight application.

How to do it...

Deploying a Silverlight application is easy; the Silverlight code is compiled and wrapped into a *.xap file. Getting this file on the client side and running it from there is our only concern. The following steps are to be carried out for deploying a Silverlight application:

We'll use the DeployHelloWorld application to demonstrate deployment, which is available with the code downloads in the Chapter01/DeployHelloWorld folder. Build the application and notice that Visual Studio has created a *.xap file in the ClientBin directory. This file, which is nothing more than a *.zip file but with another extension, contains the assembly (one or more) to which our Silverlight application was compiled, optional resources, and the AppManifest.xaml file.While looking at the files created by default by Visual Studio in the web project, a sample HTML (DeployHelloWorldTestPage.html) and ASPX (DeployHelloWorldTestPage.aspx) page are created for us as shown in the following screenshot:Both pages have an OBJECT tag included. One of the parameters is named as Source