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 Tuesday, January 08, 2008 10:00 AM by jesseliberty

Comments

# You can retrieve the Silverlight control using GetHost()

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

Wednesday, January 09, 2008 5:24 PM by DotNetKicks.com

# Did You Know That... The Code Behind is Just... Code?

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

Sunday, February 03, 2008 1:34 PM by Blogs

# Did You Know... How to create XAML objects in Javascript?

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

Monday, February 04, 2008 1:14 PM by Blogs

# Tips of the Day: Setting properties in custom controls in 1.0

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

Tuesday, February 05, 2008 2:10 PM by Jesse Liberty - Silverlight Geek

# Did You Know That... The Code Behind is Just... Code?

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

Tuesday, February 05, 2008 2:43 PM by Jesse Liberty - Silverlight Geek

# Tips of the Day: Setting properties in custom controls in 1.0

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

Tuesday, February 05, 2008 3:08 PM by Jesse Liberty

# Webcast Follow-up - CreateFromXAML

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

Wednesday, February 06, 2008 6:27 PM by Jesse Liberty - Silverlight Geek

# Webcast Follow-up - CreateFromXAML

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

Wednesday, February 06, 2008 7:29 PM by Blogs

# Webcast Follow-up - CreateFromXAML

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

Wednesday, February 20, 2008 2:55 AM by Mirrored Blogs

# Tips of the Day: Setting properties in custom controls in 1.0

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

Wednesday, February 20, 2008 2:55 AM by Mirrored Blogs

# Did You Know... How to create XAML objects in Javascript?

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

Wednesday, February 20, 2008 2:55 AM by Mirrored Blogs

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

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

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

Friday, April 10, 2009 7:54 AM by koistya