| More Resources
Downloads |
n addition to the XAML and JavaScript combination supported in Silverlight 1.0, this Silverlight 2.0 Alpha client also supports managed code written in a language such as C# or VB.NET. That single addition brings many more development possibilities into play, and also pulls in a host of exisiting .NET developers, who can immediately take advantage of the new managed code feature.
As an illustration of the power available by combining .NET code, JavaScript, XAML, animations, web services, isolated storage, and of course, HTML, this article shows you how to build a "user viewer" application using Microsoft Silverlight 2.0 that displays user information via a rich and intuitive interface. The user viewer application shows a list of users in a card-like view, much like a Rolodex. Each card contains baseline information about a user. When a user can select a specific card to view, it flies out and enlarges, displaying a detail view.
Running the application—or any Silverlight application—requires only that the client machine has the appropriate Silverlight plug-in installed. When clients without the correct plug-in attempt to run the application they are prompted to download that plug-in version. Silverlight currently supports all major browsers on both Windows and Mac OSX.
Silverlight applications can be hosted on any web server, because they are just serving out HTML. However, if your Silverlight application is embedded within an ASP.NET application, or it uses ASP.NET AJAX Web services (as the sample application does), you'll have to host it on IIS.
Expression Blend is a great tool for designing your user interface and animations, but you will need to use Visual Studio to edit the managed code to go along with it. Note that Blend does not currently include Intellisense support for XAML, but Visual Studio does, so you will also want to use Visual Studio when writing XAML manually.
| What You Need |
| To develop Silverlight 2.0 Alpha applications you need: |
![]() | |
| Figure 1. Changing Opacity: Hovering over a card brings it forward and increases its opacity. |
// Keep a reference to parent canvas for later
// use in finding child objects.
_control = (Canvas)this.InitializeFromXaml(
new StreamReader(s).ReadToEnd());
There are a couple of things to keep in mind when creating a custom control. All custom controls must be compiled as embedded resources, which allows the Silverlight client to access their XAML via the manifest resource stream of the assembly: Stream s = this.GetType().Assembly.
GetManifestResourceStream(
"UserViewer.UserCardControl.xaml");
Be very careful when you copy and paste an existing control to create a new one; it's easy to forget to change the file reference, which means you'll load the XAML for the wrong control. If you look into the manifest collection you will see all the XAML files for your application's controls. You can inspect all the available resource streams by drilling into the following collection: string[] manifestResources =
this.GetType().Assembly.GetManifestResourceNames();
Download the Silverlight sample User Viewer application.
| More Resources
Downloads |
Loading Card Data
The application retrieves the data for the card list from a standard ASP.NET AJAX Web service. You need to add the ScriptService attribute to the Web service class and the ScriptMethod attribute to the methods of that class to allow Silverlight to call the service methods.
Currently, Silverlight does not allow cross-domain service calls, so any services you wish to consume must be hosted on the same domain as your Silverlight application. However, you can get around this restriction by simply employing an intermediary service hosted on the same domain as your Silverlight application that calls out to a remote Web service.
User Settings
It's useful to let users control the maximum number of cards to display in the rolodex view. The application supports that feature via a simple class that exposes a publicly assessable property. Rather than making a web service call every time the application needs these settings, it's much more efficient to read them from the client. This technique is where isolated storage comes to the rescue.
Silverlight 2.0 provides access to Microsoft's isolated storage functionality. The isolated storage functionality provided by Silverlight is based on the isolated storage functionality in the .NET framework.
| Author's Note: Isolated storage access is available only in Silverlight 2.0 version through managed code; there is no JavaScript equivalent, so Silverlight 1.0 does not have this ability. |
IsolatedStorageFile isoStore =
IsolatedStorageFile.GetUserStoreForApplication();
IsolatedStorageFileStream isoStream =
new IsolatedStorageFileStream(_settingsFilename,
FileMode.Open, isoStore))
Using that file stream you can read the isolated storage data; in this case the application reads that data into a string: JavaScriptSerializer jsonSerializer =
new JavaScriptSerializer();
String buffer = reader.ReadToEnd();
_currentUserSettings = jsonSerializer.Deserialize
<UserManagementSettings>(buffer);
The application serializes data in the settings file using JavaScript object notation (JSON), a format that's much less verbose than XML. ASP.NET AJAX Web services use the JSON serializer to decrease the size of data sent over the wire. protected void frontSide_MouseEnter(
object sender, MouseEventArgs e)
{
// Start the animation that raises the
// opacity of the card.
_animShowCard.Begin();
_control.SetValue<double>(
Canvas.ZIndexProperty, 99);
}
protected void frontSide_MouseLeave(
object sender, EventArgs e)
{
// Start the animation that lowers the opacity of the card.
_animHideCard.Begin();
_control.SetValue<double>(
Canvas.ZIndexProperty, ZIndex);
}
To start an animation, you simply get a reference to the appropriate StoryBoard object and then call the Begin method. (Storyboard objects also bubble up notification events when an animation begins or completes.)![]() | |
| Figure 2. Enlarged Card: When clicked, a "fly-out" animation enlarges the card, providing additional screen space for detail information. |
| More Resources
Downloads |
Editing Card Data
Out of the box, Silverlight includes only the most basic controls such as Rectangles, TextBlocks, and Canvases—meaning it has no built-in input functionality. As you can imagine, writing complex controls such as TextBoxes and ListBoxes yourself, from scratch, would be quite an undertaking. To circumvent that problem, Silverlight provides rich interaction with the HTML Document Object Model (DOM), giving you the ability to interact both with HTML elements already on the page, and with HTML elements you create and add to the page dynamically at run-time using the HTML DOM.
When creating a Silverlight host control you obviously need to specify certain common properties such as width, height, and version, but you also need to set some other properties to control specific aspects of Silverlight's behavior when interacting with the host HTML page and the HTML DOM. If you need to overlay HTML controls on top of the Silverlight host control you must specify a value of true for the isWindowless property. However, you should set isWindowless to true only when you truly need that functionality, because there are performance implications when running in windowless mode:
Silverlight.createObjectEx({
source: "Page.xaml",
parentElement: document.getElementById(
"SilverlightControlHost"),
id: "SilverlightControl",
properties: {
width: "100%",
height: "100%",
version: "1.1",
enableHtmlAccess: "true",
isWindowless: "true"
},
events: {}
});
![]() | |
| Figure 3. Overlaid HTML Controls: You can see the three editable HTML text input controls that have replaced the read-only Silverlight TextBlock controls on this card. |
HtmlElement txtEmailInput =
HtmlPage.Document.GetElementByID("txtEmail");
// Set value.
txtEmailInput.SetAttribute(
"value", _txtEmail.Text);
// Set style.
txtEmailInput.SetStyleAttribute(
"display", "block");
txtEmailInput.SetStyleAttribute(
"position", "fixed");
Through the HtmlElement reference, you can read or modify a control's attributes and style attributes. Attributes represent properties such as input type and value, while style attributes represent anything that would be contained within the control's style attribute in HTML. Because processing happens in the same context on the client as the hosting HTML page, when a user updates the contents of an HTML control, that update is immediately available to your managed code. To get data out of the HTML controls, you just read the value attribute; in this case you want to update the TextBlock values before minimizing the edited card, transferring any user edits back to the original card: string email =
HtmlPage.Document.GetElementByID(
"txtEmail").GetAttribute("value");
_txtEmail.Text = email;
Possible Enhancements