Getting Started with Silverlight - Part 2
[next]
Getting Started with Silverlight: Part 2
Silverlight.createObjectEx
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:
|
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
[next]
URL: