views:

632

answers:

2

I'm playing with Silverlight 3 at the mo. I'm trying to get the current user ID into the Silverlight 3 page. I've done a bit of research and initParams seems to be the right way to go. Trouble is, they seemed to use the asp:Silverlight control and that's gone in SL3. I'm stuck trying to get a variable into the initParams list, assuming this is the right way to go.

I've started with a new Silverlight 3 application called "MyFirstSilverlightApp". I've added a code-behind page to the "MyFirstSilverlightAppTestPage.aspx" to allow me to do any clever bits.

I've managed to hard-code an initParam by adding this to the params of the object defenition:

    <param name="initParams" value="userID=id42" />

In App.xaml.cs, I've added the following to Application_Startup:

    string userID = e.InitParams["userID"];

and I've passed that into my page in a parameter in the constructor and then used that in a control. That all works.

What I can't work out is how to get the value from the variable I created in the code-behind into the param name value definition. Any help would be gratefully received.

+1  A: 

One quick a dirty approach would be to use <% %> in the param:-

<param name="intiParams" value="userID=<%=myUserID%>" />

My prefered solution is to create my own Silverlight Web Control that can render the object tag and its contents in manner taylored to my application.

AnthonyWJones
Ah, yes, that works. I'd seen reference to those tags but without the = sign I couldn't get them to work. Like you said, it feels a bit grubby but it works for this.I've never written a web control - I tend to dabble in ASP.NET when I have to so I've only done basic stuff. I don't suppose you could post sample code for such a thing, could you?
serialhobbyist
A: 

Add it to your resources collection.

Try something like.

For Each item As KeyValuePair(Of String, String) In e.InitParams
    Resources.Add(item.Key, item.Value)
Next
Martin
I'm guessing you haven't understood my problem. Either that, or I haven't understood your solution. e.InitParams is available in Application_Startup, isn't it? I need to get the parameter into e.InitParams, not from e.InitParams. I'm trying to inject it into my Silverlight app.
serialhobbyist
You're right sorry, I misread your post. this refers to handling the param once it's passed into your app.
Martin