Silverlight 2 & CreateSilverlight() - DevCorner

Silverlight 2 & CreateSilverlight()

Current Silverlight 2 Beta by default doesn't using silverlight.js/CreateSilverlight() functionality we used to add Silverlight in Alpha version, but Silverlight directly created on page as object:

   1: <div id="silverlightControlHost">
   2:        <object data="data:application/x-silverlight," type="application/x-silverlight-2-b1" width="100%" height="100%">
   3:            <param name="source" value="SilverlightApplication4.xap"/>
   4:            <param name="onerror" value="onSilverlightError" />
   5:            <param name="background" value="white" />
   6:            
   7:            <a href="http://go.microsoft.com/fwlink/?LinkID=108182" style="text-decoration: none;">
   8:                 <img src="http://go.microsoft.com/fwlink/?LinkId=108181" alt="Get Microsoft Silverlight" style="border-style: none"/>
   9:            </a>
  10:        </object>
  11:        <iframe style='visibility:hidden;height:0;width:0;border:0px'></iframe>
  12:    </div>

What to do, when we want to control more precisely the object creation, fine tune some properties or use single point of creation for multiple Silverlight components in single page?

Well, the good news, that silverlight.js is still exists (could be found in Silverlight SDK directory "%ProgramFiles%\Microsoft SDKs\Silverlight\v2.0\Tools"), and could be used to create our own createSilverlight() functions... Here is my example of such a function:

   1: //Parameters:
   2: //src - source xap file
   3: //elm - host element Id
   4: //width, height - control width/height (could be in %)
   5: //htmlAccess - allow control to interact with HTML. Values "true", "false"
   6: //windowLess - is control windowless. Values "true", "false"
   7: function createSilverlight(controlId, src, elm, width, height, htmlAccess, windowLess)
   8: {
   9:         Silverlight.createObjectEx({
  10:                 source: src,
  11:                 parentElement: elm,
  12:                 id: controlId,
  13:                 properties: {
  14:                         width: width,
  15:                         height: height,
  16:                         version: "2.0",
  17:                         enableHtmlAccess: htmlAccess,
  18:                         isWindowless: windowLess
  19:                 },
  20:                 events: { }
  21:         });
  22:         // Give the keyboard focus to the Silverlight control by default
  23:         document.body.onload = function()
  24:         {
  25:                 var silverlightControl = document.getElementById(controlId);
  26:                 if (silverlightControl)
  27:                         silverlightControl.focus();
  28:         }
  29: }

More information could be found here.

sign_speaker2

Enjoy,

Alex

Published Sunday, March 16, 2008 10:00 AM by Alex Golesh

Comments

# re: Silverlight 2 & CreateSilverlight()

Hi Alex,

Thanks for posting that.  It turned up in a search and helped clarify things.

Alan Cobb

Monday, March 17, 2008 9:39 PM by Alan Cobb

# re: Silverlight 2 & CreateSilverlight()

You welcome :)

Tuesday, March 18, 2008 6:03 AM by Alex Golesh

# Resources for geekSpeak: Building Animations in Silverlight with Cal Schrotenboer

Here is some follow up information from Cal Schrotenboer's webcast on Silverlight animation . Cal provided

Monday, March 24, 2008 11:23 PM by geekSpeak

# MSDN Blog Postings &raquo; Resources for geekSpeak: Building Animations in Silverlight with Cal Schrotenboer

Pingback from  MSDN Blog Postings  &raquo; Resources for geekSpeak: Building Animations in Silverlight with Cal Schrotenboer

# MSDN Blog Postings &raquo; Resources for geekSpeak: Building Animations in Silverlight with Cal Schrotenboer

Pingback from  MSDN Blog Postings  &raquo; Resources for geekSpeak: Building Animations in Silverlight with Cal Schrotenboer

# Semi transparent Silverlight on top of HTML

My last post was about laying Silverlight on top of HTML , using CSS for positioning and ordering of

Tuesday, April 22, 2008 6:43 PM by Web Development Adventures in a .NET world

Leave a Comment

(required) 
(required) 
(optional)
(required) 

Enter the numbers above: