Page view counter

Silverlight Tips of the Day - Blog by Mike Snow

Game Programming with Silverlight

Silverlight Tip of the Day #57 – How to Dynamically Load a Silverlight Control within another Silverlight Control

English  Spanish  French

Let’s say your site is entirely written in Silverlight but you want to be able to load and run a different Silverlight application within your main Silverlight application/site. Currently this scenario is not directly supported in Silverlight. However, I have a work around that will more or less accomplish this scenario.

The way I do this is to add a second, hidden Silverlight control to your web page. I set the Source for this second Silverlight control to be empty (“”) until I want the Silverlight control to load and display. I also put the control in a DIV that is hidden. You can load and unload this control as well and you can dynamically set it to point to different XAP’s to load different applications in your site.

Step 1.

In my web site (I.e. default.aspx file):

Main Silverlight Control that represents my web site:

<div style="height: 100%; z-index: 1; position: absolute;">
    <asp:Silverlight ID="Xaml1" runat="server" Source="~/ClientBin/SLDev.xap"  MinimumVersion="2.0.30523" Width="100%" Height="100%" />
</div>

Second Silverlight control that we will load and display within the main site:

<div id="myDIV" style="z-index: 2; display: none; position: absolute; padding-left: 0px;   padding-top: 200px;">
    <asp:Silverlight ID="Silverlight1" runat="server" Source="" MinimumVersion="2.0.30523" Width="800px" Height="600px" />
</div>

Things to notice:

  1. I set the style z-index order to be higher for the second Silverlight control than the first so that it appears on top.
  2. I set style display = none for the DIV of the second Silverlight control so that it is not displayed and does not interfere with mouse/keyboard input for the first Silverlight control.
  3. I set style position = absolute so that the controls can float on top of each other. You will want to adjust the padding-left and padding-right to properly position the control where you want it on your site.
  4. I set Source =”” for the second Silverlight control to keep it from loading an application until I am ready.

Step 2.

In my web site (I.e default.aspx) I add a two Javascript functions that my Silverlight application can call to load and hide the second Silverlight control. Few notes:

  1. LoadSilverlightControl() – This function takes a parameter that contains the full path to the XAP we want to load. Example: “ClientBin\TankWar.xap”
  2. For the Silverlight Control, I call setAttribute() to change the source of the Silverlight control.
  3. For the DIV I modify style.display setting it to “block” so that it will display or “none” to hide it.
<script type="text/javascript">
    function LoadSilverlightControl(ctrl) {
 
        var control = document.getElementById("Silverlight1");
        var div = document.getElementById("myDIV");
        div.style.display = "block";        
        control.setAttribute("Source", ctrl);
    }
 
    function HideSilverlightControl() {
 
        var control = document.getElementById("Silverlight1");
        var div = document.getElementById("myDIV");
        div.style.display = "none";
        control.setAttribute("Source", "");
    }
</script>

Step 3.

The call from Silverlight->Javascript to make the control load and run:

HtmlPage.Window.Invoke("LoadSilverlightControl", "ClientBin/TankWar.xap");

To hide it:

HtmlPage.Window.Invoke("HideSilverlightControl");

Result.

The following screen shots show this in action. Click on the “Games” button….

image 

And my Tank War application is loaded and displayed.

image

Thank you,
--Mike Snow

 Subscribe in a reader

Comments

Microsoft Weblogs said:

Let’s say your site is entirely written in Silverlight but you want to be able to load and run a different

# October 8, 2008 5:36 PM

wopos said:

good,thanks

# October 8, 2008 6:39 PM

Visual Web Developer Team Blog said:

Silverlight Tip of the Day #57 Title: How to Dynamically Load a Silverlight Control within another Silverlight

# October 8, 2008 11:05 PM

Ayush Singhal said:

Its good..

Thats what i m also trying..

Thanks

# October 8, 2008 11:16 PM

sariel said:

thanks for this I had asked a few others how to do this.  Is there anyway that the 2+ controls can share Isolated Storage ?

# October 8, 2008 11:21 PM

RIA Revolution » Dynamically Load One Silverlight Control within Another said:

Pingback from  RIA Revolution  &raquo; Dynamically Load One Silverlight Control within Another

# October 8, 2008 11:50 PM

Community Blogs said:

In this issue: Tim Heuer, Mike Snow, Peter Bromberg, Jeff Handley, Terence Tsang, Corey Schuman, and

# October 9, 2008 12:51 AM

2008 October 09 - Links for today « My (almost) Daily Links said:

Pingback from  2008 October 09 - Links for today &laquo; My (almost) Daily Links

# October 9, 2008 2:39 AM

Perfect Manhattan said:

Pingback from  Perfect Manhattan

# October 9, 2008 3:12 AM

Silverlight news for October 9, 2008 said:

Pingback from  Silverlight news for October 9, 2008

# October 9, 2008 9:06 AM

Dew Drop - October 9, 2008 | Alvin Ashcraft's Morning Dew said:

Pingback from  Dew Drop - October 9, 2008 | Alvin Ashcraft's Morning Dew

# October 9, 2008 9:58 AM

mike.snow said:

Sariel - I am not aware of any way for two separate controls to share the same Isolated Storage. For security reasons I belive they are kept private to the individual apps.

# October 9, 2008 8:08 PM

codebased said:

you can do that through dynamically loading of two xap file and read all dlls and load into the memory once it is done all yo have to do is create an object and show it :)

# October 11, 2008 5:20 AM

Mirrored Blogs said:

Post: Approved at: Oct-11-2008 Silverlight 2.0 to be Released to the Wild Monday? It appears that Silverlight

# October 11, 2008 10:48 AM

Dew Drop - October 9, 2008 | Alvin Ashcraft's Morning Dew said:

Pingback from  Dew Drop - October 9, 2008 | Alvin Ashcraft's Morning Dew

# October 12, 2008 6:02 PM

Silverlight Tip of the Day #57 ??? How to Dynamically Load a Silverlight Control within another Silverlight Control - Silverlight Tips of the Day - Blog by Mike Snow said:

Pingback from  Silverlight Tip of the Day #57 ??? How to Dynamically Load a Silverlight Control within another Silverlight Control - Silverlight Tips of the Day - Blog by Mike Snow

# October 14, 2008 4:51 PM

MS Tech News » Silverlight Tips of the Day ??? Week 8 said:

Pingback from  MS Tech News &raquo; Silverlight Tips of the Day ??? Week 8

# October 27, 2008 3:25 PM

MS Tech News » Silverlight Tips of the Day ??? Week 8 said:

Pingback from  MS Tech News &raquo; Silverlight Tips of the Day ??? Week 8

# October 27, 2008 3:31 PM

Astuce Silverlight : comment charger dynamiquement un contr??le Silverlight dans un autre contr??le Silverlight ? - #S001 | MS Tech News said:

Pingback from  Astuce Silverlight : comment charger dynamiquement un contr??le Silverlight dans un autre contr??le Silverlight ? - #S001 | MS Tech News

# October 28, 2008 2:32 AM

Toutes les astuces pour mieux utiliser Visual Studio : Astuce Silverlight : comment charger dynamiquement un contrôle Silverlight dans un autre contrôle Silverlight ? - #S001 said:

Pingback from  Toutes les astuces pour mieux utiliser Visual Studio  : Astuce Silverlight : comment charger dynamiquement un contr&#244;le Silverlight dans un autre contr&#244;le Silverlight ? - #S001

# November 12, 2008 5:15 AM