Passing InitParams through the <OBJECT> tag...
Last post 06-12-2008 6:43 PM by ebirrell. 11 replies.
Sort Posts:
04-24-2008 8:00 AM
Passing InitParams through the <OBJECT> tag...

Hi there,

on page http://msdn2.microsoft.com/en-us/library/system.windows.startupeventargs.initparams(VS.95).aspx, it says that "The initialization parameters are passed as the initParams parameter of initialization functions such as Silverlight.CreateObject or Silverlight.CreateObjectEx, or potentially set directly on the OBJECT/EMBED tag.

All my attempts to pass parameters through the <OBJECT> tag fail. Does anyone have a working code sample? 

I am working with Silverlight 2.0. If this is not yet implemented in the current version of Silverlight, does anyone know when it will be implemented?

Can anyone help?

A

 

 

BlueAquarius

Loading...
Joined on 04-24-2008
Posts 33
04-24-2008 10:55 AM
Marked as Answer
Re: Passing InitParams through the <OBJECT> tag...

 Can you show us the code that you wrote?

If you read this "Instantiating a Silverlight Plug-In (Silverlight 2) "  from Silverlight doc, you should be able to pass the initparams through the <OBJECT>

I just tested it. It is working fine..

SilverlightApplication8TestPage.html

 <body>
    <!-- Runtime errors from Silverlight will be displayed here.
    This will contain debugging information and should be removed or hidden when debugging is completed -->
    <div id='errorLocation' style="font-size: small;color: Gray;"></div>

    <div id="silverlightControlHost">
        <object data="data:application/x-silverlight," type="application/x-silverlight-2-b1" width="100%" height="100%">
            <param name="source" value="ClientBin/SilverlightApplication8.xap"/>
            <param name="onerror" value="onSilverlightError" />
            <param name="background" value="white" />
            <param name="initParams" value="color=white" />
            
            <a href="http://go.microsoft.com/fwlink/?LinkID=108182" style="text-decoration: none;">
                 <img src="http://go.microsoft.com/fwlink/?LinkId=108181" alt="Get Microsoft Silverlight" style="border-style: none"/>
            </a>
        </object>
        <iframe style='visibility:hidden;height:0;width:0;border:0px'></iframe>
    </div>
</body>

App.xaml.cs

private void Application_Startup(object sender, StartupEventArgs e) {          
            this.RootVisual = new Page();
            var color = e.InitParams["color"];
}

(If this has answered your question, please click on "Mark as Answer" on this post. Thank you!)

Best Regards,
Michael Sync

Blog : http://michaelsync.net
Feed : http://michaelsync.net/feed

mchlSync

Loading...
Joined on 09-16-2005
Singapore
Posts 2,312
04-24-2008 12:00 PM
Re: Passing InitParams through the <OBJECT> tag...

Thanks for your help. This really works. The mistake which I have made was that I did not use the equal sign ('=') within the initParams definition. Stupid mistake. 

BlueAquarius

Loading...
Joined on 04-24-2008
Posts 33
04-24-2008 12:08 PM
Re: Passing InitParams through the <OBJECT> tag...

BlueAquarius:
Thanks for your help. This really works.
 

Your welcome. Please mark my post as an answer if it's helpful.  

(If this has answered your question, please click on "Mark as Answer" on this post. Thank you!)

Best Regards,
Michael Sync

Blog : http://michaelsync.net
Feed : http://michaelsync.net/feed

mchlSync

Loading...
Joined on 09-16-2005
Singapore
Posts 2,312
06-11-2008 8:45 PM
Re: Passing InitParams through the <OBJECT> tag...

I'm trying to do exactly this and it isn't working. I've tried just copy-pasting the code from your previous post, and I get the same response: e.InitParams.Count = 0.

 Any ideas what might be going on?

Thanks

ebirrell

Loading...
Joined on 06-11-2008
Posts 13
06-11-2008 11:04 PM
Re: Passing InitParams through the <OBJECT> tag...

Can you please share your code (the HTML Page with the <OBJECT> tag as well as the App.cs file).

BlueAquarius

Loading...
Joined on 04-24-2008
Posts 33
06-11-2008 11:07 PM
Re: Passing InitParams through the <OBJECT> tag...

... and which version of Silverlight are you using? Last Friday, a new version (Silverlight 2.0 Beta 2) came out.

BlueAquarius

Loading...
Joined on 04-24-2008
Posts 33
06-12-2008 12:49 PM
Re: Passing InitParams through the <OBJECT> tag...

I believe I'm using Silverlight 2 Beta 1. I got it two weeks ago.

I'm just trying to get the example from above to work. Here is the html I'm using: (sorry about the lack of indenting)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >

<!-- saved from url=(0014)about:internet -->

<head>

<title>Silverlight Project Test Page </title>

<style type="text/css">

html, body {

height: 100%;

overflow: auto;

}

body {

padding: 0;

margin: 0;

}

#silverlightControlHost {

height: 100%;

}

</style>

 

<script type="text/javascript">

function onSilverlightError(sender, args) {

if (args.errorType == "InitializeError") {

var errorDiv = document.getElementById("errorLocation");

if (errorDiv != null)

errorDiv.innerHTML = args.errorType + "- " + args.errorMessage;

}

}

</script>

</head>

<body>

<!-- Runtime errors from Silverlight will be displayed here.

This will contain debugging information and should be removed or hidden when debugging is completed -->

<div id='errorLocation' style="font-size: small;color: Gray;"></div>

<div id="silverlightControlHost">

<object data="data:application/x-silverlight," type="application/x-silverlight-2-b1" width="100%" height="100%">

<param name="source" value="ClientBin/ParamsTest.xap"/>

<param name="onerror" value="onSilverlightError" />

<param name="background" value="white" />

<param name="initParams" value="color=white" />

 

<a href="http://go.microsoft.com/fwlink/?LinkID=108182" style="text-decoration: none;">

<img src="http://go.microsoft.com/fwlink/?LinkId=108181" alt="Get Microsoft Silverlight" style="border-style: none"/>

</a>

</object>

<iframe style='visibility:hidden;height:0;width:0;border:0px'></iframe>

</div>

</body>

</html>

 

And here is the App.xaml.cs file:

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Documents;

using System.Windows.Input;

using System.Windows.Media;

using System.Windows.Media.Animation;

using System.Windows.Shapes;

namespace ParamsTest

{

public partial class App : Application

{

public App()

{

this.Startup += this.Application_Startup;

this.Exit += this.Application_Exit;

this.UnhandledException += this.Application_UnhandledException;

InitializeComponent();

}

private void Application_Startup(object sender, StartupEventArgs e)

{

this.RootVisual = new Page();var color = e.InitParams["color"];

}

 

 

private void Application_Exit(object sender, EventArgs e)

{

}

private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)

{

}

}

}

 

html, body { height: 100%; overflow: auto; } body { padding: 0; margin: 0; } #silverlightControlHost { height: 100%; } function onSilverlightError(sender, args) { if (args.errorType == "InitializeError") { var errorDiv = document.getElementById("errorLocation"); if (errorDiv != null) errorDiv.innerHTML = args.errorType + "- " + args.errorMessage; } }

ebirrell

Loading...
Joined on 06-11-2008
Posts 13
06-12-2008 1:26 PM
Re: Passing InitParams through the <OBJECT> tag...

ebirrell,

thanks for sharing your code. However, from your code I am not able to understand the following:

  • What is not working? Does the computer crash? Do you get an error message? It does not compile? What is really your problem, other than it does not work? Please be as exact as possible.
  • What do you expect your application to do? Your cs-file does not really contain much code. You are assigning the parameters to a local var named color. Then nothing else happens. You never use color.
  • Are you confusing C# and VB? In your cs-file, you have one line which says var color = e.InitParams["color"];. This looks very much like a VB line. I am not aware of var in C#.  

 

BlueAquarius

Loading...
Joined on 04-24-2008
Posts 33
06-12-2008 5:14 PM
Re: Passing InitParams through the <OBJECT> tag...

1) It compiles. However, when I try to run this code, it throws a KeyNotFound exception on the line var color = e.InitParams["color"] (and the VS debugger says that the dictionary e.InitParams has zero keys). I do not know why it didn't put the color key in the dictionary or how to fix it (or get anything else to go into the dictionary).

2) I expect my application to load a blank page. (As you said there isn't much code, I'm just trying to isolate the problem). However it just gets as far as the line var color = e.InitParams["color"] and throws the exception.

3) Quite possibly. I'm new to both of them. If you can suggest a valid line of C# code that will access the value of any key in e.InitParams, that would be great.

 Thanks again for your help!

ebirrell

Loading...
Joined on 06-11-2008
Posts 13
06-12-2008 6:00 PM
Re: Passing InitParams through the <OBJECT> tag...

I do not really see why it would not work. The only thing which I wouldchange is convert var to String. But that's just a personal preference.

 

But here is another questions.... Are you absolutely sure that when you execute your project, you definitely run the HTML Code which is pasted above? When you create a Silverlight application in Visual Studio, the template creates two test pages for you: an HTML Page and an ASPX page. maybe you have added the line <param name="initParams" value="color=white" /> to the HTML Page and then execute the ASPX page.

 

To be absolutely sure: right-click on the HTML page and select 'View in Browser'.

BlueAquarius

Loading...
Joined on 04-24-2008
Posts 33
06-12-2008 6:43 PM
Re: Passing InitParams through the <OBJECT> tag...

Ah, you're right. I didn't notice that.

 Thanks!

ebirrell

Loading...
Joined on 06-11-2008
Posts 13
Page view counter