如何从ASP中获取一个变量到Silverlight 3控件。NET代码?

时间:2022-03-14 06:26:41

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.

我在mo玩Silverlight 3,我试图把当前用户ID放到Silverlight 3页面。我做了一些研究,initParams似乎是正确的方法。问题是,他们似乎使用了asp:Silverlight控件,这在SL3中已经没有了。我一直试图将一个变量放到initParams列表中,假设这是正确的方法。

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.

我从一个名为“MyFirstSilverlightApp”的新Silverlight 3应用程序开始。我在“MyFirstSilverlightAppTestPage”中添加了一个代码后面的页面。让我做一些聪明的事情。

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

我已经设法硬编码了一个initParam通过添加到对象防御的params:

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

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

在App.xaml。我在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.

我搞不懂的是如何从我在代码背后创建的变量中获取值到参数名值定义中。我们将感激地接受任何帮助。

2 个解决方案

#1


2  

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

一个简单的方法是在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.

我的首选解决方案是创建我自己的Silverlight Web控件,它可以使对象标记和它的内容在我的应用程序中呈现。

#2


0  

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

#1


2  

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

一个简单的方法是在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.

我的首选解决方案是创建我自己的Silverlight Web控件,它可以使对象标记和它的内容在我的应用程序中呈现。

#2


0  

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