views:

1030

answers:

2

I have two silverlight instances embedded in a page like follows (content simplfied to relevant details only):-

<object id="SL1" data="data:application/x-silverlight-2," type="application/x-silverlight-2">
   <param name="source" value="/clientBin/Common.xap"></param>
   <param name="minRuntimeVersion" value="3.0.40624.0"></param>
   <param name="windowless" value="True"></param>
   <param name="onload" value="silverlight_onload"></param>
   <param name="initParams" value="some configuration here"></param>
 </object>

<object id="SL2" data="data:application/x-silverlight-2," type="application/x-silverlight-2">
   <param name="source" value="/clientBin/Common.xap"></param>
   <param name="minRuntimeVersion" value="3.0.40624.0"></param>
   <param name="windowless" value="True"></param>
   <param name="onload" value="silverlight_onload"></param>
   <param name="initParams" value="a different configuration here"></param>
 </object>

As you can see both objects point at the same xap but will have different content in the initParams.

Note, however, that the onload param points to the same function name. This particular part of this question is invariant.

Elsewhere in javascript at the global level I have:-

function silverlight_onload(sender, eventargs)
{
  //Magic pixie dust here
}

The goal is to take the sender and determine which of the object elements is the source of the event. The straight-forward (but for the sake of this question unacceptable) answer would be to use two different functions. I'm look for something that does not require a new function be added to the global namespace per SL object.

Bonus, kudos goes to anyone who can tell me why SL couldn't get a little more assistance from the host browser and handle the onload parameter more like other element events like onlick. For example ask the browser to eval("function() {" + onload + "}" and attach the return function to the onload event.

A: 

Well your first point of call should be the silverlight.js reference.

Basically your "silverlight_onload" javascript function is going to get as sender the actual html object element, so check what it's id is and you'll have what I think you want.

The reason SL can't get more assistance from the host is that the html object doesn't have an onload event. Object Element reference.


Well since the documentation doesn't seem to reflect reality you're left calling the javascript method from within the silverlight control itself. Just pass in an id as one of the init params.

Graeme Bradbury
It would be neat if the sender in the onload event were the object element unfortunately that is not the case. It has the type "UserControl" and the Name "Layout" root. However I can't figure out how to navigate from that to the containing object element.
AnthonyWJones
+2  A: 

Is your goal to get the initparams in the javascript and then do something with it? If so you can do:

sender.getHost().InitParams

the getHost() returns the actual plugin object see msdn page for more details. I don't know of a way to get the object's ID unless you set it as an initparam.

Gus
getHost() is what I'm after, it returns the specific object element hosting the plugin currently running the event. Hence I can use this to direct program flow to the correct code for the element.
AnthonyWJones