28 September 2008
Just because you can do something doesn't mean that you should do something. Just because you can get Quake to run on your iPhone doesn't mean that's the best way to do things. But it's pretty cool!
Now that we're clear on that one, this is the part where I show you that you can do a ton of stuff in Silverlight, through the JavaScript interoperability features.
Reaching out to the browser can help you improve the fit and finish of your Silverlight application and to integrate with existing services.
So, if you can do it in AJAX/JavaScript, you can totally do it in your Silverlight application when it is hosted in a web browser.
Reasons not to use the "HTML DOM bridge" (JavaScript interoperability features) in Silverlight:
Reasons to use it:
What follows is a list of some previously covered articles and examples. They may be out of date; for instance, in the Release Candidate of Silverlight, you now use the ScriptObjectCollection instead of the HtmlElementCollection. Mike Snow has a nice list of the breaking changes.
Access and create JavaScript arrays from managed code
You can access JavaScript arrays, but because of the loosely-defined, "do anything you want in JavaScript" way that things work, it leaves you with pretty unattractive managed code.
VB samples are here. Some C# samples:
// Create an array var array = (ScriptObject) HtmlPage.Window.Eval("[10, 9, 8, 7, 6 ];"); // Get the length of an array int length = (int)((double)array.GetProperty("length")); // Return a single element by index var singleElement = array.GetProperty(index.ToString())
Resize the Silverlight plugin
You can change the size of the plugin and its host element. Here's some Visual Basic that makes a "zero-pixel" plugin:
Imports System.Windows.Browser Namespace System.Windows.Browser Public Class ZeroPixelPlugin ''' <summary> ''' Resizes the Silverlight plugin and its host element to be zero pixels ''' in width and height. Only really useful for HTML ''' </summary> ''' <remarks></remarks> Public Shared Sub HideSilverlightPlugin() Dim plugin As HtmlElement = HtmlPage.Plugin Dim host As HtmlElement = plugin.Parent plugin.SetStyleAttribute("width", "0") plugin.SetStyleAttribute("height", "0") host.SetStyleAttribute("width", "0") host.SetStyleAttribute("height", "0") End Sub End Class End Namespace
Get information about the browser and physical screen
Wondering what the color depth is of your end users? Want to work with the available screen size? All this information is exposed by JavaScript and you can easily read the values in C#.
Here's a simple static class that does the work for you. The class takes care of the differences between the NPAPI web browsers and Internet Explorer, so your application doesn't need to worry about it so much.
Store text in the clipboard
If you have information that you want your end users to be able to copy into the clipboard, you only have a few options:
Here's a solution that uses the browser APIs in IE, and another plugin if you're on a non-IE browser. It isn't perfect, but it's an interesting hack.
Generate an entire HTML/AJAX-style user interface
You can write complete applications, just like an AJAX app, using managed code. Your business logic and other components benefit from strongly typed, easy-to-edit C#. Your application can be delivered through a .Xap, and doesn't have any server side dependencies if it is written just right.
Here was a homepage "link cloud" app. It hasn't been updated in a while, but most of the code should work without changes in the release candidate.
Some of the things the app and the post show:
Interface with web analytics software
A while back (yeah, Silverlight 1.1 days) I posted about interfacing with Google Analytics / Urchin. Although the information is still useful, Google did make some major updates to the APIs, so I'd recommend not using the Urchin implementation in the post.
Anything else cool?
If you're using the DOM bridge to do some interesting stuff, feel free to post a link here - I'd like to post about the neat uses at some point in the near future.
Well, I hope that gets you started working with the interop features a little more. Done right, you can start wearing a T-shirt the says "I do it in JavaScript... AND in .NET!"
Jeff Wilcox is a Software Engineer at Microsoft in the Open Source Programs Office (OSPO), helping Microsoft engineers use, contribute to and release open source at scale.