Creating an IronRuby Silverlight Application

IronRuby is an implementation of the Ruby programming language that is currently being developed by Microsoft. Using IronRuby, developers can implement Ruby code as the client-side language for Silverlight applications. Using Ruby to code Silverlight applications allows developers to use already existing Ruby applications to provide rich functionality to the Silverlight application. The first step in creating an IronRuby Silverlight application is to create a folder for the application. Inside...

Adding load and error events to Silverlight at initialization time

The events parameter allows you to specify functions that are executed on certain events during the Silverlight application initialization. Currently, Silverlight allows you to register handlers for onLoad and onError events. The following code snippet creates a Silverlight object that implements onLoad and onError event handling lt script Silverlight.createObjectEx source Page.xaml, parentElement id SilverlightControl, properties width 400, height 400, version 1.1, background 1111FF onLoad...

n irr

Name SilvefligtitApplicationl Location C boofcs srlvertight cti04 code From the New Project window, select Silverlight 2 Application from the project type selections. Specify the name and location to create the project. Then specify the programming language that is used to provide functionality to the project. Currently, the options are C and Visual Basic. Then click OK to create the new project. A new project is created and the Page.xaml file is displayed in the design window, as shown in...

Adding dynamic XAML using managed JavaScript

You can use managed JavaScript code to dynamically add XAML objects that are rendered to the browser in the Silverlight application. This is one of the most powerful features of the DLR because it allows you to employ the strengths of the scripting language to create a content-rich interface for the user. One of the biggest strengths of the DLR is that almost every Silverlight object has a constructor. You can use that constructor rather than having to use CreateFromXaml or a XamlReader to...

Creating a manifest for the managed JavaScript Silverlight application

With the XAML file in place, you should create an XML file named AppManifest.xml. Although this file is created automatically by Chiron, Chiron does not automatically add all of the assembly libraries you need. Listing 11.10 shows an example of an AppManifest.xml file for a managed JavaScript Silverlight application. This manifest file includes the standard Silverlight assemblies, the assemblies for managed JavaScript, and the assemblies to implement the Silverlight 2 controls. Example...

Encoding Metadata in the Media File

The biggest benefit to developers using Expression Encoder to encode audio and video is the ability to encode metadata into the output file. Using Silverlight, that metadata can be extracted later and used to implement rich features in the video playback for example, displaying chapter titles and embedding script commands that trigger application events at specific points in the media playback. Metadata can be added to media files in Expression Encoder by clicking the Metadata tab, as shown in...

Handling the animation completed event

The Storyboard object provides the Completed event, similar to the Completed event that is provided by the MediaElement control. When the Storyboard is completely rendered, the Completed event is triggered. The Completed event is very useful when you need to do cleanup work after an event or if you want to run multiple animations in a sequence. For example, consider two Storyboard objects named animateFirst and animateSecond. If you want to run the animations in a sequence one after another,...

Specifying the source parameter

The source parameter of the createObjectEx function specifies the location of the Silverlight XAP assembly or the XAML file that should be used to initially render the Silverlight object. Typically, the value of source is a URL that points to an XAP assembly file, as shown in the following line of code source The value of source can also be a reference to XAML that is embedded in the HTML document. The following code snippet shows an example accessing embedded XAML in an HTML document lt script...

Application startup

After the Silverlight application has been created using the constructor, the application startup method is called prior to allowing the user access. This allows Silverlight applications to perform any initialization activities, such as processing initialization parameters, initializing resources or properties, loading data, and displaying the application UI. The following code in Listings 7.23 and 7.24 shows the implementation of a basic application startup method. XAML Code for a Basic...

Providing access to CLR libraries and namespaces in IronRuby

With the AppManifest.xml created, you can begin creating an IronRuby application by creating a file named app.py in the same folder as the XAML file. The first step in Ruby programming is to add references to the libraries that are consumed by the application. To provide access to the assemblies in your IronRuby program, you need to use a require statement to add a reference to the assembly. For example, to provide access to the Controls and Controls.Extended libraries, use the following code...

Creating a manifest for the IronRuby Silverlight application

With the XAML file in place, you should create an XML file named AppManifest.xml. Although this file is created automatically by Chiron, Chiron does not automatically add all of the assembly libraries you need. Listing 11.7 shows an example of an AppManifest.xml file for an IronRuby Silverlight application. This manifest file includes the standard Silverlight assemblies, the assemblies for IronRuby, and the assemblies to implement the Silverlight 2 controls. Example AppManifest.xml File for an...

DLR Console on the Web

There is a great Web site that you can use to play around with DLR languages in Silverlight applications. The folks at dynamicsilverlight.net have created a Web-based console utility that allows you to add XAML and DLR code and have it dynamically applied to a Canvas element that is part of the same Web page. This allows you to test code snippets and play around with the language. The address of the DLR console utility, shown in Figure 11.2 is A DLR console utility on the Web that allows...

Starting and stopping the animation

When an animation is started, Silverlight begins rendering the animation as a sequence of frames to the browser based on the duration set in the animation. What is animated is defined by any DoubleAnimation or DoubleAnimationWithKeyFrame children that belong to the Storyboard. Starting and stopping animations is handled through the Begin and Stop functions of a Storyboard object. For example, if you define a Storyboard with the name myAnimation, then the following lines of C code start the...

Defining Storyboard controls

The Storyboard control acts as a container control for animation controls and allows you to set properties that apply to the animation. The most commonly used properties of the Storyboard control are the AutoReverse, BeginTime, RepeatBehavior, and Duration. The BeginTime property specifies the time, in hours minutes seconds format, in the Storyboard timeline to begin playback of the animation. If the AutoReverse property is set to True, the animation first plays forward and then immediately...

Implementing a Silverlight library class with codebehind pages

As with application assemblies, .NET Silverlight library classes are actually spread across two source files an XAML file and a C or Visual Basic code-behind file. The first step in implementing a .NET Silverlight library is to link a C or Visual Basic code-behind page to a Silverlight XAML application. Code-behind pages are managed code files that are a partial subset of a class defined in XAML code that is used to implement a Silverlight application or user control. In Silverlight libraries,...

Configuring userdefined parameters in the Silverlight application

The initParams parameter allows you to add custom defined parameters that get passed to the Silverlight application when it is initialized. These parameters can be accessed using the initParams attribute of the Silverlight control object. The following example code adds a list of comma-delineated strings as the initParms setting for the Silverlight object and then uses the Silverlight control object in an onLoad event handler to access the list of strings Silverlight.createObjectEx source...

Understanding XML Namespaces

A very important aspect of XAML that you need to be familiar with is the concept of namespaces. An XML namespace is a method to avoid element name conflicts when using multiple libraries. XML namespaces are implemented in Silverlight XAML files by adding an xmlns URI attribute to the root UserControl element for each library that you are using in the file. For example, the following line of code adds the XAML presentation namespace to an XAML file When using multiple Silverlight namespaces in...

Adding Silverlight plugin events

You also need to add the onError and onResize parameters to point to error and resize event handlers as well as an lt IFRAME gt . The event handler parameters specify the name of event handler functions, either in JavaScript or in managed code, to call when an error occurs. Listing 7.35 shows a fully deployed Silverlight application including JavaScript onError and onResize event handler functions. Silverlight Application Embedded in a Web Page lt DOCTYPE html PUBLIC - W3C DTD XHTML 1.0...

Setting up the Silverlight project to use the LINQ service

When the Web server side of the Silverlight project is wired, configure the Silverlight application itself to consume the data exposed by the LinqToSql service. The following sections take you through the process of referencing the WCF service and binding the SQL data to a DataGrid control. The first step in accessing the SQL data from a Silverlight application is to add a reference to the IService1 Web service. This is done by right-clicking the References folder in the Silverlight project and...

Other control events

There are several events that are limited to specific Silverlight controls. These events provide functionality specific to the type of control that is being implemented for example, the DateSelected event is used to determine when a date is selected in the Silverlight Calendar control. Table 8.6 describes the different types of special events that can be applied to a specific Silverlight control. Triggered when the drop-down calendar is opened in a DatePicker Triggered when the drop-down...

Enabling Network Security Access for Silverlight Applications

Whenever you are dealing with network communication, especially for Web applications, you should be concerned with security. The Silverlight framework implements a rigid security scheme to protect services from network threats such as denial of service, DNS rebinding, and reverse tunneling. The simplest way to protect against such attacks is to prevent any cross-domain access to services and to limit access to the host or at least the site of origin. This works well for many services however, a...

Accessing XAML Using Unmanaged JavaScript

One of the most powerful features of Silverlight is the ability to access and programmatically change a Silverlight application after it is loaded. Accessing the Silverlight application can be broken down into three main areas accessing the Silverlight object, accessing the Silverlight application settings, and accessing the XAML content. You should already be familiar with the Silverlight object from earlier in this chapter. The Silverlight application settings define the look and behavior of...

Implementing library assemblies

Another useful Silverlight feature is the ability to develop Silverlight library assemblies. These libraries make it possible to break applications down and create reusable controls that can be consumed by multiple applications. Library assemblies can be referenced by application assemblies as well as other library assemblies. This enables you to provide additional functionality to an application as part of the application package or on demand, allowing developers to create applications that...

Encoding Multiscale Images Using Deep Zoom Composer 1

One of the coolest advancements for images in Web applications is the concept of a multiscale image. Multiscale images are designed to provide a means to implement high resolution images in Web pages without causing severe bandwidth issues. A multiscale image is actually a series of images that represent different zoom states of a single original. Using the different zoom levels of a multiscaled image provides the ability to display an image and then zoom in on it without losing resolution. The...

Creating a library assembly

For .NET Silverlight applications, you need to compile the library class into an application assembly. If you use Visual Studio, the ability to build the assembly is automatically built into your Silverlight projects. If you are not using a Visual Studio Silverlight project, use MSBuild to compile your library class. You need to provide MSBuild with an XML project configuration file similar to the one in Listing 7.11 that is borrowed from Microsoft's MSDN Web site Sample MSBuild Configuration...

Serializing objects into XML

The first step in serializing objects into XML is to create an object that can be serialized. This is done by creating a simple class and then implementing XML tag decorators that are exposed by the System.Xml.Serialization library to define the XML properties of the class. The following is a list of the XML tag decorators that you can use to define XML properties of a class XmlAttribute Defines a member as an XML attribute XmlElement Defines a member as an XML element Xmllgnore Specifies that...

Summary Vea

This chapter focused on using Silverlight's presentation framework to interact with the HTML DOM. Using the Browser library, you can access elements in the DOM directly from Silverlight applications. Also, by exposing Silverlight applications as scriptable, you can access Silverlight functions from HTML JavaScript. Silverlight provides the OpenFileDialog class to provide read-only access to client-side files. Silverlight also provides the IsolatedStorageFile class to access the isolated local...

Setting the layout of controls in the Silverlight application

The most common settings that you work with in Expression Blend are the layout settings available on the Layout pane, as shown in Figure 4.31. These properties determine the layout size, positioning, and behavior of the controls. The following sections discuss the different settings available in the Layout pane. Setting the Layout properties of a control in the Layout pane of the Properties tab in Expression Blend The first settings in the Layout pane are the Width and Height textboxes. The...

Implementing the custom control functionality

After you add the UI elements to the generic.xaml file, you can begin implementing the functionality of the control in the control class file. The first step is to modify the class definition to specify that the class derives from the Control class. The following C code illustrates setting the NumScroll class to derive from Control After you specify that the custom control class derives from Control, you can begin implementing properties and event handling to add functionality to the control....

Dynamically creating animations in code

Defining animations in XAML works well for most button animations however, there are times that you want to dynamically create animations in your .NET code. This allows you to reuse the same animation for multiple objects easily and gives you much more flexibility in defining the behavior of the animation. Creating an animation programmatically uses much of the same logic as creating one in XAML. You create a Storyboard object and then add to it DoubleAnimation or DoubleAnimationWithKeyFrame...

Creating a ToggleButton

ToggleButton controls are very basically a cross between a Button control and a CheckBox control. They inherit all of the Button controls' properties and events however, they also inherit the state functionality of a CheckBox control. When the user clicks on a ToggleButton control and releases the mouse, the ToggleButton stays in a pressed state. This allows you to implement button elements that set on or off type values. Just as with CheckBox controls, the state of the ToggleButton is...

XAP application package

The Silverlight application package is basically a ZIP file with a .xap extension. Packaging the Silverlight application into a package provides a number of benefits. Packages are much easier to distribute, they are easier to manage, large programs can be broken down into a series of smaller packages, and it is very easy to share packages between Silverlight applications. You need to package the application assembly output into an application package so that your .NET Silverlight application...

Silverlight Design Fundamentals

Using XAML to Build Declarative Silverlight Applications Using Expression Blend to Design Silverlight Applications Encoding Audio, Video, and Images for Silverlight Applications

Dynamically loading text and media

Another common use of the Downloader object is to dynamically download a package that contains related content of different types and then implement that content to update or modify the Silverlight application. Using the Downloader to download zipped files gives you the flexibility to create packages that include text, XAML, media, and images that are related, and only download those that are necessary during runtime. The code in Listings 9.15 and 9.16 provides an example of how to dynamically...

Setting Silverlight plugin attributes

After you add an lt OBJECT gt tag to the Web page, add the id, data, type, height, and width attributes, as described in Table 7.1, to configure the Silverlight plug-in as shown in the following code lt div id mySilverlightControlHost gt lt object height 100 width 100 gt lt object gt lt div gt Table 7.1 describes the attributes that you should add to an embedded Silverlight plug-in lt OBJECT gt tag. Attributes of the Silverlight Plug-in lt OBJECT gt Tag Required. Sets the name for the plug-in...

Modifying the Properties of Silverlight Controls

After adding Silverlight controls to the application using the Toolbox, you can modify the properties of a selected control using the Properties tab, as shown in Figure 4.23. The Properties tab displays a series of panes that provide a graphical means of modifying the attributes of the Silverlight controls. In Chapter 2, we showed you how to set the properties of a Silverlight control using XAML code. You will quickly see how using Expression Blend to set properties is much faster and easier...

Manipulating XAML Elements Programmatically

A very useful feature of code-behind pages when programming .NET Silverlight applications is the ability to access and manipulate the controls defined by XAML elements. XAML is very limited when it comes to providing functionality to a Web site. That is where the code-behind pages come in. With the code-behind pages, you can programmatically access XAML elements to modify the user interface dynamically to provide a much better experience for the user. The following sections take you through the...

Creating a HyperlinkButton

The HyperlinkButton control is kind of a hybrid cross between a button and text. The control appears similar to a TextBlock however, it has mouse clicking functionality of a Button control built into it. What makes the HyperlinkButton control unique is the NavigateUri property. You can set the NavigateUri property to any URI, as shown in the following code. When the HyperlinkButton is clicked, the browser navigates to that URI lt HyperlinkButton Content Silverlight Link Height 50 Width 150...

Accessing an ASPNET Web service from Silverlight

Using the service reference created in the previous section, you are able to access the WebService from the managed code in your Silverlight application as a SOAP client. Using a SOAP client, managed code is able to access the WebMethod functionality exposed by the WebService. Add code to implement service reference namespace The first step in providing access to the WebService in your managed code is to include a reference to the namespace specified when creating the service reference. For...

Sending data on a socket

To send data on a socket, use the SendAsync method. As with the ConnectAsync method, you need to set up a SocketAsyncEventArgs object again. The UserToken property must be set to the Socket object and the RemoteEndPoint property must be set to the endpoint object. If you are sending data in the connection completed event handler function, you can use the SocketAsyncEventArgs object that is passed as the second argument to the handler. The UserToken and RemoteEndPoint properties are already set...

Dynamically creating XAML elements

The simplest way to create XAML elements is to generate them dynamically in managed code and then add them to an existing XAML element as a child. XAML elements that can contain child elements such as a Grid or Canvas have a Children object as an attribute when they are exposed in the namescope. You can use the Add method of the Children object to add objects to an existing object. For example consider the following code that exposes a Canvas object using the name blank-Canvas lt Canvas x name...

Applying brushes to controls

One of the biggest advantages to Silverlight is the ability to apply brushes to controls that modify the color and gradient. Using brushes gives you unlimited possibilities when designing the look and feel of your Silverlight applications. The Brushes pane, shown in Figure 4.25, is made up of two main sections. The top section is a list of the available properties of the control that can accept some kind of brush setting. For example, Figure 4.25 shows the Brushes pane for a TextBox control....

Creating XAML for the IronPython Silverlight application

In your application folder, create a subfolder named app. In the app subfolder, create the XAML file containing the visual layout for the IronPython Silverlight application. We recommend using the UserControl as the root visual element for the XAML file. Listing 11.3 shows an example XAML file that can be used to implement an IronPython Silverlight application. The code in Listing 11.3 implements a TextBlock control named titleBlock, a Button control named myButton, and a Canvas control named...

Defining the UI for the custom control

After creating and configuring the Silverlight class library project as described in the previous section, you can begin adding UI elements to the generic.xaml file. This can be done in one of two ways. The first way is to simply add the XAML necessary for the library. The second way is to implement a ResourceDictionary element that contains a Style element that can be used as a custom template control. The second method takes a bit more code, but it enables application developers who use the...

Exporting XAML from Expression Design

The first step in adding vector art to Silverlight applications is to add the artwork to Expression Design. This can be done by creating the artwork in Expression Design. You can also create the artwork in another application that can export the vector image as an Adobe Illustrator AI file and then import the artwork into Expression Design by choosing File C Import from the main menu. After the artwork is added to Expression Design, you can export it as XAML code. You can either export the...

Implementing Event Handling

One of the strengths of Silverlight is how easy it is to implement event handling in applications. Event handling is accomplished by attaching a specific event handler to an object. In Silverlight, this can be done either in the XAML code or in the managed code-behind file. In XAML, an event handler can be attached to an object by setting the event attribute to the name of the handler function in the code-behind page. For example, the following code attached the MouseEnterHandler function to...

Creating a MediaElement

Another useful Silverlight control for adding a professional look to applications is the MediaElement control. Just as with the Image control, the MediaElement control allows you to add audio and video to your applications as a code-manageable control by simply referencing their URI in the Source property. The Height and Width properties allow you to set the size of the MediaElement control, and the Stretch property works the same as with Image controls. Much of the work with MediaElement...

Dynamically deleting XAML elements

Using code-behind pages, you can also dynamically remove XAML elements from an object. This is done by accessing the Children attribute of an XAML element that contains child elements. You can use the Remove method of the Children object to remove child objects from an existing object. For example, consider the following code that exposes a Canvas object using the name myCan-vas, the canvas contains a child TextBlock object name txtBlock lt Canvas x Name myCanvas gt lt TextBlock x Name txtBlock...

Properties Gfu

ActualHeight, ActualWidth, Clip, Cursor, DataContext, DesiredSize, Dispatcher, Fill, GeometryTransform, Height, HorizontalAlignment, IsHitTestVisible, Language, Margin, MaxHeight, MaxWidth, MinHeight, MinWidth, Name, Opacity, OpacityMask, Parent, RenderSize, RenderTransform, RenderTransformOrigin, Resources, Stretch, Stroke, StrokeDashArray, StrokeDashCap, StrokeDashOffset, StrokeEndLineCap, StrokeLineJoin, StrokeMiterLimit, StrokeStartLineCap, StrokeThickness, Style, Tag, Triggers,...

Configuring parameters of the createObject and createObjectEx functions

The createObject and createObjectEx functions perform the same basic task, the only difference is how parameters are passed to them. The createObject function accepts the parameters using basic comma-separated syntax as follows createObject Source, Parent, ID, Properties, Events, initParams, Context The createObjectEx function accepts the parameters using a single JavaScript Object Notation JSON dictionary that contains the parameters as a single package, as shown in the following...