Did You Know That… the second way to pass information to your Silverlight control is through the seventh (context) parameter

In the previous Tip of the Day, we looked at the sixth parameter to createSilverlight/ createSilverlightEx which allowed you to pass in a string as the initParams parameter.

You can also pass in a seventh parameter of type object, which is known as the user context. This can be any object you like. However, rather than retrieving it as a property of the Silverlight control, you retrieve the userContext as the second argument passed to the load event handling method

handleLoad: function(plugIn, userContext, rootElement)

As a simple example, you can create an array in default.html.JS. and pass that in as your userContext object:

var authorArray = 
   [ "Douglas Adams", "Neil Gaiman", "Neal Stephenson" ];

context: authorArray

Since this becomes available to you only in handleLoad you'll save it in a member variable so that it will be available in every member method:

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

Using the keyword this creates a member variable that is accessible from any member method. The userContext looks like a variable, but what you passed in is an array, and so you can use it as an array.

handleMouseUp: function(sender, eventArgs) 
{
    alert("this.userContext[0] = " + this.userContext[0]);
    alert("this.userContext[1] = " + this.userContext[1]);
    alert("this.userContext[2] = " + this.userContext[2]);
},

 

I will be posting a "How Do I" video on using the sixth and seventh parameter of CreateObject, very soon.

Share

About Jesse Liberty

Jesse Liberty is an independent consultant and programmer with three decades of experience writing and delivering software projects. He is the author of 2 dozen books and multiple Pluralsight courses, and has been 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, Microsoft MVP and Telerik MVP.
This entry was posted in z Silverlight Archives. Bookmark the permalink.

Leave a Reply

Your email address will not be published.