方法1:
向嵌入aspx页面的silverlight对象添加imnitParams参数,它的value是与querystring类似的键值对,多个参数用逗号隔开,aspx后台代码如下所示:
<
object data=
"data:application/x-silverlight-2,"
type=
"application/x-silverlight-2"
width= "100%" height= "100%">
< param name= "source" value= "ClientBin/Para_from_aspx2sl.xap" />
< param name= "onError" value= "onSilverlightError" />
< param name= "background" value= "white" />
< param name= "minRuntimeVersion" value= "3.0.40624.0" />
< param name= "autoUpgrade" value= "true" />
< param name= "initParams" value= "id=114,name=lensso" />
<!--或以如下形式给value赋值-->
<!--<param name="initParames" value="id=<%# Request.Querystring["id"]%>"/>-->
< /object>
width= "100%" height= "100%">
< param name= "source" value= "ClientBin/Para_from_aspx2sl.xap" />
< param name= "onError" value= "onSilverlightError" />
< param name= "background" value= "white" />
< param name= "minRuntimeVersion" value= "3.0.40624.0" />
< param name= "autoUpgrade" value= "true" />
< param name= "initParams" value= "id=114,name=lensso" />
<!--或以如下形式给value赋值-->
<!--<param name="initParames" value="id=<%# Request.Querystring["id"]%>"/>-->
< /object>
至此完成了aspx页面的赋值工作,在silverlight中的Application_Startup事件处理函数中可以利用事件变量来取得该值,(App.Xaml.cs)代码如下:
private
void Application_Startup(
object sender, StartupEventArgs e)
{
this.RootVisual = new MainPage();
MessageBox.Show(e.InitParams[ "id"]+e.InitParams[ "name"]);
}
{
this.RootVisual = new MainPage();
MessageBox.Show(e.InitParams[ "id"]+e.InitParams[ "name"]);
}
方法2:
1.下载System.Web.Silverlight.dll(可以去网上搜一下)
2.将下载的dll引用进web工程
3.在aspx页面添加注册代码,如下:
<
%@ Register Assembly=
"System.Web.Silverlight"
Namespace= "System.Web.UI.SilverlightControls" TagPrefix= "sl" %>
Namespace= "System.Web.UI.SilverlightControls" TagPrefix= "sl" %>
4.添加silverlight控件,代码如下:
<
sl:Silverlight id=
"slobj"
runat=
"server"
source=
"~/ClientBin/Para_from_aspx2sl.xap"
version= "2.0" width= "100%" height= "100%">< /sl:Silverlight>
version= "2.0" width= "100%" height= "100%">< /sl:Silverlight>
5.添加scriptmanager,代码如下
<
asp:scriptmanager ID=
"Scriptmanager1"
runat=
"server"><
/asp:scriptmanager>
6.aspx后台代码传值,代码如下:
slobj.InitParameters =
"id=123,name=hello";
7.silverlight中取得aspx传过来的值,代码如方法1。
<注>如果你想把配置放在web.config中,在aspx的后台添加配置,而不想使用sl控件,可以使用aspx的literal控件,如下方法:
web.config配置
1 <
appSettings>
2 < add key= "AuthenticationAddress" value= "http://192.168.104.133:13999/SysAdmin/Runtime/AuthenticationService.svc" />
3 < /appSettings>
2 < add key= "AuthenticationAddress" value= "http://192.168.104.133:13999/SysAdmin/Runtime/AuthenticationService.svc" />
3 < /appSettings>
aspx中的写法如下
1 <
object data=
"data:application/x-silverlight-2,"
type=
"application/x-silverlight-2"
2 width= "100%" height= "100%">
3 < param name= "source" value= "ClientBin/OIU.xap" />
4 < param name= "onError" value= "onSilverlightError" />
5 < param name= "background" value= "white" />
6 < param name= "minRuntimeVersion" value= "3.0.40818.0" />
7 < param name= "autoUpgrade" value= "true" />
8 < asp:Literal ID= "slInitParams" runat= "server">< /asp:Literal>
9 < a href= "http://go.microsoft.com/fwlink/?LinkID=149156&v=3.0.40818.0" style= "text-decoration: none">
10 < img src= "http://go.microsoft.com/fwlink/?LinkId=161376" alt= "获取 Microsoft Silverlight"
11 style= "border-style: none" />
12 < /a>
13 < /object>
14 < iframe id= "_sl_historyFrame" style= "visibility: hidden; height: 0px; width: 0px;
15 border: 0px">< /iframe>
2 width= "100%" height= "100%">
3 < param name= "source" value= "ClientBin/OIU.xap" />
4 < param name= "onError" value= "onSilverlightError" />
5 < param name= "background" value= "white" />
6 < param name= "minRuntimeVersion" value= "3.0.40818.0" />
7 < param name= "autoUpgrade" value= "true" />
8 < asp:Literal ID= "slInitParams" runat= "server">< /asp:Literal>
9 < a href= "http://go.microsoft.com/fwlink/?LinkID=149156&v=3.0.40818.0" style= "text-decoration: none">
10 < img src= "http://go.microsoft.com/fwlink/?LinkId=161376" alt= "获取 Microsoft Silverlight"
11 style= "border-style: none" />
12 < /a>
13 < /object>
14 < iframe id= "_sl_historyFrame" style= "visibility: hidden; height: 0px; width: 0px;
15 border: 0px">< /iframe>
注意第8行添加的控件
aspx后台这么写
1 StringBuilder sb =
new StringBuilder();
2 sb.Append( " <param name=\"InitParams\" value=\"");
3 sb.Append( string.Format( "AuthenticationAddress={0}", ConfigurationManager.AppSettings[ "AuthenticationAddress"]));
4 sb.Append( "\"></param>");
5 slInitParams.Text =sb.ToString();
2 sb.Append( " <param name=\"InitParams\" value=\"");
3 sb.Append( string.Format( "AuthenticationAddress={0}", ConfigurationManager.AppSettings[ "AuthenticationAddress"]));
4 sb.Append( "\"></param>");
5 slInitParams.Text =sb.ToString();
同样可以实现动态配置silverlight的服务。