Getting Started with Silverlight - Part 2 | WebReference

Getting Started with Silverlight - Part 2


Getting Started with Silverlight: Part 2

Adam Nathan

Digg This Add to


Silverlight.js defines a second function for embedding Silverlight content called Silverlight.createObjectEx. (The Ex suffix is an old Win32 convention that has mysteriously made its way into this file. It typically denotes a newer or "extra" version of a function.) The only difference between createObject and createObjectEx is that the latter accepts a single associative array parameter with all the same information. For example, here is the previous call to createObject translated into a call to createObjectEx:

The nice thing about createObjectEx is that calls to it are self-descriptive. You can clearly see what piece of data is the source, parentElement, and so on without the need for comments. For this reason, examples in this book use createObjectEx rather than createObject. The syntax for calling createObjectEx might look unusual, but it's basically JSON (JavaScript Object Notation), a popular data interchange format based on simple JavaScript constructs.

Digging Deeper

The Implementation of createObjectEx
createObjectEx is a very simple wrapper over createObject, as you can see by looking at its source code inside Silverlight.js. It is effectively implemented as follows:

In JavaScript, syntax such as a.b is equivalent to a["b"], which is why params.source can be used to access the source element of the params array, and so on.

WARNING: When calling createObject or createObjectEx, some properties and events can't be omitted!

If you omit the version property, you'll get a script error; and if you omit either the width or height, the resultant element won't be seen. As for events, you must at least specify an empty associative array ({}); otherwise, you'll get a script error.

Putting It All Together

The createObject or createObjectEx function can be called from any JavaScript file or inline SCRIPT element, but Microsoft has published the following recommended approach for using these functions:

  1. Create a separate script file called CreateSilverlight.js (by convention).
  2. Define a parameterless function (called createSilverlight by convention) inside CreateSilverlight.js that makes the call to createObject or createObjectEx.
  3. Reference both Silverlight.js and CreateSilverlight.js from SCRIPT elements in your HTML document (usually inside the document's HEAD).
  4. Place an HTML element that you want to contain the Silverlight content, such as a DIV, inside the document and choose an id (used by your createSilverlight function).
  5. Call the parameterless function inside inline JavaScript in the HTML document.

Listings 1.4 and 1.5 follow this approach to get the same result pictured in Figures 1.1 and 1.3.

LISTING 1.4 Embedding Silverlight Content Using the Recommended Silverlight.js Approach

LISTING 1.5 CreateSilverlight.js—The Recommended Script File with the Parameterless createSilverlight Function