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

About Jesse Liberty

Jesse Liberty has three decades of experience writing and delivering software projects and is the author of 2 dozen books and a couple dozen Pluralsight & LinkedIn Learning courses. He was a Senior Technical Evangelist for Microsoft, a Distinguished Software Engineer for AT&T, a VP for Information Services for Citibank and a Software Architect for PBS. He is a Xamarin Certified Mobile Developer and a Xamarin MVP and a Microsoft MVP.
This entry was posted in z Silverlight Archives. Bookmark the permalink.