Page view counter

Did You Know That... you can retrieve a reference to the Silverlight control using GetHost()

In many examples you will see the Silverlight control passed into handleLoad as the plugIn parameter, and stored as a member variable,

handleLoad: function(plugIn, userContext, rootElement) 
{
    this.plugIn = plugIn;

This is typically done so that you can use the plug-in object in other member methods, such as event handlers.

var rect = this.plugIn.content.FindName("myRect");
rect["Canvas.Left"] -= 50;

An alternative, however, is not to store plugIn as a member variable, but rather to retrieve a reference to the Silverlight control when you need it, by calling GetHost() on any convenient  Silverlight element

handleLoad: function(plugIn, userContext, rootElement) 
{
    var rect = plugIn.content.FindName("myRect");
    rect["Canvas.Left"] = 300;
    
    rect.addEventListener("MouseLeftButtonUp", Silverlight.createDelegate(this, this.handleMouseUp));

},

handleMouseUp: function(sender, eventArgs) 
{
   var plugIn = sender.GetHost();
   var rect = this.plugIn.content.FindName("myRect");
   rect["Canvas.Left"] -= 50;
}
kick it on DotNetKicks.com
Published 08 January 2008 10:00 AM by jesseliberty

Comments

# DotNetKicks.com said on 09 January, 2008 05:24 PM

You've been kicked (a good thing) - Trackback from DotNetKicks.com

# Blogs said on 03 February, 2008 01:34 PM

A very insightful reader sent me email that I'm going to respond to here. To protect his identity

# Blogs said on 04 February, 2008 01:14 PM

I have had a very strong positive reaction to focusing on the Javascript in code behind for Silverlight

# Jesse Liberty - Silverlight Geek said on 05 February, 2008 02:10 PM

Got a nice international email today that said in part... ...things get more interesting it is a common

# Jesse Liberty - Silverlight Geek said on 05 February, 2008 02:43 PM

A very insightful reader sent me email that I'm going to respond to here. To protect his identity

# Jesse Liberty said on 05 February, 2008 03:08 PM

Got a nice international email today that said in part... ...things get more interesting it is a common

# Jesse Liberty - Silverlight Geek said on 06 February, 2008 06:27 PM

Today's Webcast focused on factoring out repeated creation of objects in XAML (and their manipulation

# Blogs said on 06 February, 2008 07:29 PM

Today's Webcast focused on factoring out repeated creation of objects in XAML (and their manipulation

# Mirrored Blogs said on 20 February, 2008 02:55 AM

Today's Webcast focused on factoring out repeated creation of objects in XAML (and their manipulation

# Mirrored Blogs said on 20 February, 2008 02:55 AM

Got a nice international email today that said in part... ...things get more interesting it is a common

# Mirrored Blogs said on 20 February, 2008 02:55 AM

I have had a very strong positive reaction to focusing on the Javascript in code behind for Silverlight

# koistya said on 10 April, 2009 07:54 AM

Great tip. Going to use it in "jQuery SL" plugin:

code.google.com/.../jquery-sl