silverlight 可以通过属性InitParams 获得参数,如果参数是动态的需要web程序传递的,具体操作如下:
web程序后台:
this.frmReport.Attributes["src"] = String.Format("../../SilverlightApplicationTestPage.aspx?AppID={0}&USERID={1}", AppID, this.CurrentUserInfo.CurrUserInfo.USERID.ToString());
silverlight项目会生产一个 测试页面文件:SilverlightApplicationTestPage.aspx,代码如下:
<%@ Page Language="c#" AutoEventWireup="true" %>
<!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">
<head
id="Head1" runat="server">
<title>SilverlightApplication</title>
<style type="text/css">
html, body
{
height:
100%;
overflow: auto;
}
body
{
padding:
0;
margin:
0;
}
#silverlightControlHost
{
height:
100%;
text-align: center;
}
</style>
<script
type="text/javascript" src="Silverlight.js"></script>
<script type="text/javascript">
function onSilverlightError(sender, args)
{
var appSource
= "";
if (sender
!= null && sender != 0)
{
appSource =
sender.getHost().Source;
}
var errorType =
args.ErrorType;
var iErrorCode = args.ErrorCode;
if (errorType ==
"ImageError" || errorType == "MediaError")
{
return;
}
var errMsg = "Silverlight 应用程序中未处理的错误 " + appSource + "\n";
errMsg += "代码: "
+ iErrorCode + "
\n";
errMsg +=
"类别: " + errorType + "
\n";
errMsg +=
"消息: " + args.ErrorMessage + " \n";
if (errorType ==
"ParserError")
{
errMsg += "文件: " + args.xamlFile + "
\n";
errMsg += "行: " + args.lineNumber + "
\n";
errMsg += "位置: " + args.charPosition + "
\n";
}
else if
(errorType == "RuntimeError")
{
if (args.lineNumber != 0)
{
errMsg += "行: " + args.lineNumber + "
\n";
errMsg += "位置: " + args.charPosition + "
\n";
}
errMsg += "方法名称: " + args.methodName + "
\n";
}
throw new
Error(errMsg);
}
//加载参数
function WindOnLoad()
{
var xmlDoc;
var initParamsString = ‘‘;
var url = location.href;
var paraString = url.substring(url.indexOf("?") + 1,
url.length).split("&");
for (var i = 0; i < paraString.length; i++) {
initParamsString += paraString[i];
if (i != paraString.length) {
initParamsString += ",";
}
}
SetInitParams(initParamsString);
}
//构造页面
function
SetInitParams(initParamsString)
{
var
silverlightControlHtml = ‘ <object id="silverlightControl"
data="data:application/x-silverlight-2,"
type="application/x-silverlight-2" width="100%"
height="100%">‘;
silverlightControlHtml += ‘<param name="source"
value="ClientBin/SilverlightApplication2.xap"
/>‘;
silverlightControlHtml += ‘<param name="onerror" value="onSilverlightError"
/>‘;
silverlightControlHtml += ‘<param name="minRuntimeVersion"
value="5.0.61118.0"
/>‘;
silverlightControlHtml += ‘ <param name="autoUpgrade" value="true"
/>‘;
silverlightControlHtml += ‘<param id="InitParams" name="InitParams" value="‘
+ initParamsString + ‘"/>‘;
silverlightControlHtml +=
‘</object>‘;
silverlightControlHtml += ‘<iframe id="_sl_historyFrame" style="visibility:
hidden; height: 0; width: 0; border: 0px"></iframe>‘;
document.getElementById("silverlightControlHost").innerHTML =
silverlightControlHtml;
}
window.onload = WindOnLoad;
</script>
</head>
<body>
<div
id="silverlightControlHost">
</div>
</body>
</html>
silverlight程序会生成app.xaml 文件作为起始页面,代码如下:
public App()
{
this.Startup +=
this.Application_Startup;
this.UnhandledException += this.Application_UnhandledException;
InitializeComponent();
}
//在Application_Startup方法中得到web程序的参数,具体代码如下:
private void Application_Startup(object sender, StartupEventArgs
e)
{
paras = e.InitParams;
AppID = e.InitParams["AppID"];
USERID =
e.InitParams["USERID"];
this.RootVisual = new BeginHome(AppID,
USERID);//跳转到其他页面
}