views:

16

answers:

2

Hi,

I am new to silverlight. Excuse me if this is simple question.

I am trying to craete a sample application using Silverlight 4 in VS2010. The code that is generated by defualt in the aspx page is(apart from the script):

<div id="silverlightControlHost">
    <object data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="100%" height="100%">
      <param name="source" value="ClientBin/test.xap"/>
      <param name="onError" value="onSilverlightError" />
      <param name="background" value="white" />
      <param name="minRuntimeVersion" value="3.0.40624.0" />
      <param name="autoUpgrade" value="true" />
      <a href="http://go.microsoft.com/fwlink/?LinkID=149156&amp;v=3.0.40624.0" style="text-decoration:none">
          <img src="http://go.microsoft.com/fwlink/?LinkId=108181" alt="Get Microsoft Silverlight" style="border-style:none"/>
      </a>
    </object><iframe id="_sl_historyFrame" style="visibility:hidden;height:0px;width:0px;border:0px"></iframe>
</div>

I wanted to use asp:silverlight tag, so I added the dll System.Web.Silverlight.dll (v2.0).

I got the tag and I replaced the above code to:

<asp:ScriptManager ID="ScriptManager1" runat="server" ></asp:ScriptManager>
<asp1:Silverlight ID="test" runat = "server" Source="~/ClientBin/test.xap">
</asp1:Silverlight>   

Now the above code (self generated one) works, however the asp:silverlight shows blank screen.

Also, another questions, If we have 2 or more xaml files, how to call them?? (As we jsut refer to one xap file, where to mention which xaml file should the program refer)

Thanks in advance.

+1  A: 

I think the asp:Silverlight tag is depricated, I'd go with the generated one. for the other xaml files, you have to include them in your MainPage.xaml somehow, either by navigating to them or showing them.

Alex Lo
A: 

Stick with the <object> tag to define your Silverlight application, as @Alex mentions the old Silverlight server control is deprecated - all it does is render out the object tag for you and may not declare all the parameters you need. When using it, do a right click->view source on the rendered page and see what the differences are between it and using the object tag manually.

For showing a specific xaml page inside the SL application, i am going to assume that the choice of which page to show is dictated by actions that happen outside of the app. In this case there are several options. You can use javascript to call a managed code function in the SL app and that function can show the appropriate page. You can call from the SL app back to the containing page - you can call a javascript function or access an HTML element on the page. Alternatively you could pass in the info as part of the SL app InitParams:

<param name="InitParams" value="<% =GetMyInitParams() %>" />

in the aspx page's code behind:

protected string GetMyInitParams()
{
    return "MyStartPage=Page1,SomeOtherParam=blah";
}

these InitParams are available as your StartupEventArgs in Application_Startup of the SL app:

    private void Application_Startup(object sender, StartupEventArgs e)
    {
        if (e.InitParams != null && e.InitParams.Count > 0)
        {
            foreach (string key in e.InitParams.Keys)
            {
                switch (key)
                {
                    case "MyStartPage":
                        myPageToShow = e.InitParams["MyStartPage"];
                        break;
                }
            }
        }
        this.RootVisual = new MainPage();
    }
slugster