Jesse Liberty - Silverlight Geek

By, For and About Silverlight Developers

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

Comments

DotNetKicks.com said:

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

# January 9, 2008 5:24 PM

Blogs said:

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

# February 3, 2008 1:34 PM

Blogs said:

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

# February 4, 2008 1:14 PM

Jesse Liberty - Silverlight Geek said:

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

# February 5, 2008 2:10 PM

Jesse Liberty - Silverlight Geek said:

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

# February 5, 2008 2:43 PM

Jesse Liberty said:

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

# February 5, 2008 3:08 PM

Jesse Liberty - Silverlight Geek said:

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

# February 6, 2008 6:27 PM

Blogs said:

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

# February 6, 2008 7:29 PM

Mirrored Blogs said:

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

# February 20, 2008 2:55 AM

Mirrored Blogs said:

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

# February 20, 2008 2:55 AM

Mirrored Blogs said:

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

# February 20, 2008 2:55 AM