1

I've see this article here on stack and this article on Silverlight MSDN, but they don't address Silverlight5. See below for my html host document div code, and C# code to call it. I get a "The given key was not present in the dictionary." error every time, and the .InitParams property count is 0 every time as well. Does someone know a solution for this? I feel like this is a bug in Silverlight5 or something. Also, if someone has a nice clean alternative way of getting info from the host html object tag - I would entertain that as well.

Markup:

<div id="silverlightControlHost">
        <object data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="400" height="300">
          <param name="source" value="ClientBin/MySilverlightApplication.xap"/>
          <param name="onError" value="onSilverlightError" />
          <param name="background" value="white" />
          <param name="minRuntimeVersion" value="5.0.61118.0" />
          <param name="autoUpgrade" value="true" />
          <param name="initParams" value="rootLocation=someLocation" />
          <a href="http://go.microsoft.com/fwlink/?LinkID=149156&v=5.0.61118.0" style="text-decoration:none">
              <img src="http://go.microsoft.com/fwlink/?LinkId=161376" 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>

C# retrieval from static property:

private const String rootLocation= "rootLocation";

internal String RootLocation { 

    get
    {

        String location = App.Current.Host.InitParams[rootLocation];

        return location;

    }
}

C# retrieval from AppStartUp event:

private const String rootLocation= "rootLocation";

private void Application_Startup(object sender, StartupEventArgs e)
{
    this.RootVisual = new MainPage();

    String location = e.InitParams[rootLocation];

}
Community
  • 1
  • 1
wakurth
  • 1,644
  • 1
  • 23
  • 39
  • My own code to do that uses: `var initParams = new Dictionary(Host.InitParams, StringComparer.OrdinalIgnoreCase);` to put the params into a new Dictionary that I use. I can't remember why I did that though.. – jv42 Sep 09 '12 at 07:22
  • Maybe that was just to change the StringComparer. – jv42 Sep 09 '12 at 07:23
  • I think I've found your issue: try putting `InitParams` with the proper casing! – jv42 Sep 09 '12 at 07:24
  • I have tried it with InitParams and initParams and initparams ... all to no avail. I am doing this to simply pass a variable at runtime, and wanted to not have to use a query string parameter. I may just have to resort to that in the short term until I find out what the heck I'm doing wrong :/ – wakurth Sep 10 '12 at 17:38
  • @jv42 - are you able to set values in the InitParams - param tag in the html markup itself before runtime, and then read it after? Or are you adding values after the app is running, and then reading them? – wakurth Sep 10 '12 at 17:44
  • I'm using them either in static HTML or generated in ASP.NET, both before the runtime. – jv42 Sep 10 '12 at 18:45
  • If you want to communicate between the page (Javascript probably) and the Silverlight app, you have other ways that will work (not InitParams I think). – jv42 Sep 10 '12 at 18:46

1 Answers1

3

I had the same problem following an upgrade of a solution to Silverlight 5. Turned out that although app.xaml was being called, the WEB project had lost its "Startup Project" status. Resetting this caused everything to work again.