WEBSITE

Accessing Silverlight Content with JavaScript

7/25/2010 5:07:46 PM
I_section1_d1e6918.html

We will create a number of .js files, or code-behind JavaScript files for the HTML document containing the Silverlight content, so to speak. The JavaScript code will access the Silverlight content, add new Silverlight elements, and read information about the plug-in or the content of the XAML file.

We will use the Script Web Visual Studio template as the basis for the examples in this part of the book. Of course you can achieve everything we are covering in the chapters in this part of the book using the project template, however it is a bit easier to inject JavaScript code in the Script Web template, since the sample page there is already using JavaScript code.

Accessing the Plug-in

To access Silverlight content embedded on a page, you first need to access the Silverlight plug-in. There are two ways to retrieve this information: access the plug-in from within the XAML event handler code or use the JavaScript Document Object Model (DOM). Let's start with the latter option and look at the automatically generated (thanks to the template) JavaScript code to load Silverlight content:

Silverlight.createObjectEx({
source: 'Scene.xaml',
parentElement: document.getElementById('silverlightPlugInHost'),
id: 'silverlightPlugIn',
properties: {
width: '100%',
height: '100%',
background:'white',
version: '1.0'
},
events: {
onLoad: Silverlight.createDelegate(scene, scene.handleLoad),
onError: function(sender, args) {
var errorDiv = document.getElementById("errorLocation");
if (errorDiv != null) {
var errorText = args.errorType + "- " + args.errorMessage;

if (args.ErrorType == "ParserError") {
errorText += "
File: " + args.xamlFile;
errorText += ", line " + args.lineNumber;
errorText += " character " + args.charPosition;
}
else if (args.ErrorType == "RuntimeError") {
errorText += "
line " + args.lineNumber;
errorText += " character " + args.charPosition;
}
errorDiv.innerHTML = errorText;
}
}
},
context: null
});


Note the id property. This provides the DOM ID JavaScript can use to access the plug-in:

var plugin = document.getElementById('silverlightPlugIn');

If you are using the ASP.NET ScriptManager, you can, of course, save some typing and use $get() instead of document.getElementById().

It is tempting to use the ID of the

element holding the Silverlight content (in all of this book's examples: SilverlightPlugInHost), but this will not access the plug-in itself.


Then, starting with the plugin variable, you can access quite a bit of data on the plug-in and its contents, but we will come back to that in a minute. First we will have a look at the other option to access the plug-in, from within the XAML event handling code.

Every object in the XAML has a (JavaScript) method called getHost() that also returns a reference to the plug-in. Assuming that the eventHandler() function handles any event for the XAML file, this code would appropriately fill the plugin variable:

function eventHandler(sender, eventArgs) {
var plugin = sender.getHost();
}

Once you have accessed the plug-in, you can go further. The Silverlight plug-in exposes three types of information to JavaScript:


General plug-in information

These are accessible as direct properties or methods of the plug-in object. Examples are source (the XAML source code), initParams (the set of options used when initializing the Silverlight plug-in in the createSilverlight() function), and onError (the event handler that handles errors).


Plug-in settings information

These are accessible using plugin.settings.. Examples are background (the background color of the current Silverlight content) and maxFrameRate (the maximum frame rate in frames per second).


Plug-in content information

These are accessible using plugin.content.. Examples are findName() (a method to find XAML elements by their names, just like the server-side FindName() method), fullScreen (whether to display the content in full-screen mode), and root (the root canvas element of the Silverlight content).

Other  
 
Most View
Samsung DV150F Compact Camera Review (Part 2)
Find Out The Best Bargain For Technological Products
Web Security : Seeking Design Flaws - Abusing Predictable Identifiers, Predicting Credentials, Finding Random Numbers in Your Application
Slim And Slimmer (Part 2)
Pentax K5 II - A Fast And Elegant Prosumer DSLR
A Trio From HIS: 7970 IceQ X² GHz Edition, 7950 IceQ X² Boost Clock And 7850 IceQ Turbo X Graphics Cards Review (Part 7)
Super-zoom Superstars – Q1 2013 (Part 2) : Nikon Coolpix P520, Panasonic Lumix FX200, Panasonic FZ200
Windows 8 vs OS X Mountain Lion (Part 2)
The Comparison Among 5 Full-HD Screens
ASP.NET 4 : Data Source Controls (part 1) - The Page Life Cycle with Data Binding, The SqlDataSource, Selecting Records
Top 10
Sharepoint 2013 : Farm Management - Disable a Timer Job,Start a Timer Job, Set the Schedule for a Timer Job
Sharepoint 2013 : Farm Management - Display Available Timer Jobs on the Farm, Get a Specific Timer Job, Enable a Timer Job
Sharepoint 2013 : Farm Management - Review Workflow Configuration Settings,Modify Workflow Configuration Settings
Sharepoint 2013 : Farm Management - Review SharePoint Designer Settings, Configure SharePoint Designer Settings
Sharepoint 2013 : Farm Management - Remove a Managed Path, Merge Log Files, End the Current Log File
SQL Server 2012 : Policy Based Management - Evaluating Policies
SQL Server 2012 : Defining Policies (part 3) - Creating Policies
SQL Server 2012 : Defining Policies (part 2) - Conditions
SQL Server 2012 : Defining Policies (part 1) - Management Facets
Microsoft Exchange Server 2010 : Configuring Anti-Spam and Message Filtering Options (part 4) - Preventing Internal Servers from Being Filtered