Powered by MSDN

US - English
NEW! Silverlight 5 is available Learn More

should i use multiple xap files in my aspx file? RSS

6 replies

Last post Nov 18, 2008 10:09 AM by rahoof

(1)
  • sandabh

    sandabh

    Member

    81 Points

    48 Posts

    should i use multiple xap files in my aspx file?

    Jul 30, 2008 07:01 AM | LINK

    I have a scenario where i have an asp.net website and i want to use silverlight. now i want only some silverlight controls on the page like datagrid, self created menu bar etc. Other parts of the page will be asp.net controls. To achieve this should i create silverlight user control projects in my solution and add the xap files to the silverlight control in the aspx page? Is this the best approach?

  • texmex5

    texmex5

    Contributor

    2239 Points

    382 Posts

    Re: should i use multiple xap files in my aspx file?

    Jul 30, 2008 09:18 AM | LINK

     If you don't want to develop the whole web app in Silverlight then I think your idea is the way to go. Actually I don't imagine any other way :)

  • sladapter

    sladapter

    All-Star

    43607 Points

    7907 Posts

    Re: should i use multiple xap files in my aspx file?

    Jul 30, 2008 06:43 PM | LINK

    sandabh

    I have a scenario where i have an asp.net website and i want to use silverlight. now i want only some silverlight controls on the page like datagrid, self created menu bar etc. Other parts of the page will be asp.net controls. To achieve this should i create silverlight user control projects in my solution and add the xap files to the silverlight control in the aspx page? Is this the best approach?

    Yes. you can do this.  But they are not necessarily in different Xap files.  You can create just one Silverlight application with all the controls in it. Then you only have one Xap. But in order for different Silverlight control to show up in their area correctly, you use InitParameter to specify the Application Start UserControl.

    Take a look at your App.xaml.cs Application_Startup function. You can see Page control is your Application.VisualRoot. But you can set different UserConrol to the Application.VisualRoot based on the e.InitParam. You can set the initparamter in your Silverlight Control Tag in your HTML/aspx page.

    <asp:Silverlight ID="datagrid" runat="server" InitParameters="Control=MyDataGridControl" Source="~/ClientBin/Carousel.xap"   MinimumVersion="2.0.30523" Width="100%" Height="100%" />

    <asp:Silverlight ID="menu" runat="server" InitParameters="Control="MyMenuControl" Source="~/ClientBin/Carousel.xap"   MinimumVersion="2.0.30523" Width="100%" Height="100%" />

    Change your Applicaton_Startup function:

     private void Application_Startup(object sender, StartupEventArgs e)
            {

                if (e.InitParams.ContainsKey("Control"))

                {
                    this.RootVisual = this.GetType().Assembly.CreateInstance(e.InitParams["Control"]) as FrameworkElement; // Create your Control
               }

            }

    Remember in order for above code to work, you need to set the type(YourControls).FullName in the InitParameters .

    This way only one main Silverlight project is needed, and one .Xap is generated. But you have multiple instances of the same application, each with different RootVisual control.

     

     

    Sally Xu
    Software Engineer
    Aprimo, Inc

    Please remember to mark the replies as answers if they answered your question
  • sandabh

    sandabh

    Member

    81 Points

    48 Posts

    Re: should i use multiple xap files in my aspx file?

    Jul 31, 2008 04:33 PM | LINK

    thanks.. that looks convinient.. i just had one question.. where do you exactly set the type(YourControls).FullName in the InitParameters. I mean which file..

  • sladapter

    sladapter

    All-Star

    43607 Points

    7907 Posts

    Re: should i use multiple xap files in my aspx file?

    Jul 31, 2008 04:46 PM | LINK

    If you have a UserControl called Page1,  then typeof(Page1).FullName should give you something like "YourNameSpace.Page1". So you can set your InitParameters like this:

    InitParameters="Control=YourNameSpace.Page1"

    This can make CreateInstance line work:

    this.RootVisual =  this.GetType().Assembly.CreateInstance("YourNameSpace.Page1") as FrameworkElement;

    This just a generic way to create your start up page without having to change your Application_Startup code when you add a new control.

    The file you set  InitParameters="Control=YourNameSpace.Page1" is on any ASPX page that you want to put your Silverlight tag:

     <asp:Silverlight ID="Xaml1" runat="server" InitParameters="Control=YourNameSpace.Page1"  Source="~/ClientBin/YourSilverlight.xap" MinimumVersion="2.0.30523" Width="100%" Height="100%" />

    You can also create this Sivlerlight Obejct control from your ASP.NET code. 

     If you use the HTML test page to run the test. Modify the TestSilverlight.html page like this:

    <object id="silverlight" data="data:application/x-silverlight," type="application/x-silverlight-2-b2" width="100%" height="100%" >';
               <param name="source" value="ClientBin/YourSilverlight.xap"/>
               <param name="initParams" value="Control="YourNameSpace.Page1"/>'           
                ..

    </object>

     

     

     

     

    Sally Xu
    Software Engineer
    Aprimo, Inc

    Please remember to mark the replies as answers if they answered your question
  • sandabh

    sandabh

    Member

    81 Points

    48 Posts

    Re: should i use multiple xap files in my aspx file?

    Aug 01, 2008 04:51 PM | LINK

    Awesome. Thanks!

  • rahoof

    rahoof

    Member

    3 Points

    2 Posts

    Re: should i use multiple xap files in my aspx file?

    Nov 18, 2008 10:09 AM | LINK

    Cool its working[:)]

     

    thanks..!