Getting Started with Silverlight - Part 3 | WebReference

Getting Started with Silverlight - Part 3


Getting Started with Silverlight: Part 3

Adam Nathan

Digg This Add to

Interacting with the Silverlight Control Programmatically

The OBJECT or EMBED element representing the Silverlight control (whether part of the static HTML document or dynamically injected by Silverlight.js) has an HTML id, so you can write JavaScript to retrieve the element and get or set properties on it just like any other HTML element. For example,

Because this element is an instance of the ActiveX object (or Netscape plug-in), it provides a number of useful properties, functions, and events specific to Silverlight. This element returned by document.getElementById is the same object passed as the first parameter to the onLoad event handler. However, you should avoid accessing any Silverlight-specific members on this object before the control has finished loading (and its onLoad event is raised).

The Silverlight control exposes most of its functionality via two properties: Settings and Content.

The Settings Property

Most relevant to this chapter is the control's Settings property, which defines a number of subproperties for getting or setting a number of attributes (many of which could have alternatively been set via createObject, createObjectEx, or directly on the OBJECT/EMBED element):

  • Background—The same property discussed earlier. However, this makes it easy to change the background color at any time.
  • EnableFramerateCounter—A Boolean property that toggles the display of the current frame rate in the browser's status bar. (This is potentially useful for debugging purposes.)
  • EnableRedrawRegions—Another Boolean property meant for debugging, this highlights regions of the screen that are redrawn on each frame, when set to true.
  • EnableHtmlAccess—The same property discussed earlier.
  • MaxFrameRate—The same property discussed earlier.
  • Windowless—The same as the isWindowless property discussed earlier.

For example, the EnableRedrawRegions and Background properties can be set in a Silverlight onLoad event handler as follows:

These properties, and all other members exposed on the control object, are pretty flexible. For example, they are not case sensitive. Many people prefer using lowercase names because it matches JavaScript conventions, as in the following code that produces the same result as the preceding snippet:

In addition, the Boolean properties can be set to a true or false string or to a true or false Boolean literal, and they work correctly either way.

None of the Settings members are extremely compelling, however, as it's rare you would need to retrieve or change the data after the control has loaded.