Skip to main content
Home Forums Silverlight Programming Programming with JavaScript Silverlight 3 dynamically loading the silverlight object with javascript
8 replies. Latest Post by Jonathan Shen – MSFT on October 6, 2009.
(0)
apteacher
Member
1 points
5 Posts
09-29-2009 2:45 PM |
Hello, I have been scratching my head about an issue that has perplexed me for a couple of days! I have a silverlight control that I want to generate dynamically with javascript. here are the code framents:
swidth =
Silverlight.createObjectEx(
{
source: source,
parentElement: divElement,
id: pluginid,
properties:
width: swidth,
height: sheight,
minRuntimeVersion:
},
events:
OnError: onSilverlightError
initParams: initparameters
});
}
here is an html frag
<
</
the first two hyperlink controls work just fine and if I had or intended to use static videos would be great, but my intent is to get the video information from the server and will be dynamic so somehow I need to set the initparams of the silverlight object dynamically. I can us the xsl of a data form web part to set the title of the button and build the initparms from that, the relevant code is in the doIt() function above. The code doesn't render in IE 8 or FireFox! However, if the alert is uncommented the control renders in both browsers. However, I can't have an alert box popping up to show the media! in an actual site! What am I missing here?? It appears as if the page needs something to happen (the alert box) for the Silverlight control to render.
Sledge70
Contributor
5882 points
1,042 Posts
09-30-2009 6:00 AM |
Are you setting the width and height of the actual silverlight plugin you are trying to display?
09-30-2009 12:37 PM |
I set the hieght and width in the css file:
#silverlightControlHost{
height:400px;
width:500px;
I saw another entry about a similar problem: so I also put them in an inline style on the div.
In addition in my method call I am setting them to the same height and width.
09-30-2009 12:41 PM |
I'm talking about within the silverlight control itself.
So for instance if it's showing a usercontrol set it's width in the usercontrol. It might be that your content isn't being displayed properly which results in the internals of your SL plugin being 0 width and height.
09-30-2009 4:31 PM |
Okay! I see the control itself is a builtin expression player out of the Silverlight 3 encoder templates! Easy to reuse in our own projects.
It has its height and width set to auto. I am assumming as this player can go full screen. However, it is not the control as it will play when it is not created from a javascript onclick as it will play if called like this:
< href="BLOCKED SCRIPT createSL('silverlightControlHost','500px,'400px', 'clientbin/testmediasl3.xap', 'media=bear.wmv')">Bear</a>
it won't play if called from the button click event when called like this:
<button id="myButton" ocnclick="doIt()" >Bear</button>
where doIt has the call to createSL
in any event, I changed auto to 800 by 600 on the control and tested it and it still won't display
I need this method as I can dynamically pass the parameter to the doIt function with xsl: doIt(Bear);
09-30-2009 4:34 PM |
In addition, I found that moving the testsite to my server which still has ie7 installed this works in ie7 but not firefox! If that helps narrow down the problem!
10-01-2009 9:14 AM |
Although, this discussion hasn't answered my original question I was able to get this to work cross browser using the anchor tag
<a> <xsl:attribute name="href">BLOCKED SCRIPTcreateSL('silverlightControlHost2','500px', '400px', '/videos/testmediasl3.xap','media=../videos/<xsl:value-of select="@Title"/>.wmv');</xsl:attribute><xsl:value-of select="@Title"/></a>
So the remaining question seems to be related to this post: http://blogs.microsoft.co.il/blogs/alex_golesh/archive/2009/05/01/silverlight-tip-object-creation-via-silverlight-js-ie8-amp-firefox-tweak.aspx However, it dosen't work in this case! It is odd that I am calling the same CreateSL method but it works when called from within the page from the anchor tag but not from a function call??
Jonathan...
All-Star
24939 points
2,425 Posts
10-06-2009 3:52 AM |
Hi Apteacher,
I made a test and it works on my computer. Below is the test sample.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head> <title> Create Silverlight With JS</title> <style type="text/css"> html, body { height: 100%; overflow: auto; } body { padding: 0; margin: 0; } #silverlightControlHost { height:80%; text-align:center; } </style> <script type="text/javascript" src="Silverlight.js"></script> </head> <body> <form id="form1" runat="server" style="height:100%"> <div id="silverlightControlHost"> </div> <input type="button" value="Click" onclick="createSL('ClientBin/SLTestGroup.xap')"/> <script language="javascript" type="text/javascript"> function createSL(source) { Silverlight.createObjectEx({ source: source, parentElement: document.getElementById("silverlightControlHost"), id: "sltest", properties: { width: "640", height: "480", background: "white", alt: "<!--not installed-->", version: "3.0.40624.0" }, events: { //onError: onSLError, //onLoad: onSLLoad }, initParams:"param1=1", context: "row4" }); } </script> </form> </body> </html>
Best regards,
Jonathan
10-06-2009 3:59 AM |
Hi,
apteacher:mediaCurrent = "media=" + mediaCurrent + ".wmv";createSL('silverlightControlHost', '500px', '400px', 'clientbin/testmediasl3.xap', mediaCurrent);
Please replace it with
mediaCurrent = "media='" + mediaCurrent + ".wmv'";
Please let me know the result.