silverlight 可以通过属性InitParams 获得参数,如果参数是动态的需要web程序传递的,具体操作如下:
web程序后台:AppID,USERID需要的参数
this.frmReport.Attributes["src"] = String.Format("../../SilverlightApplicationTestPage.aspx?USERNAME={0}&USERID={1}", "张三", "123456");
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 + " ";
errMsg += "代码: " + iErrorCode + "
";
errMsg += "类别: " + errorType + "
";
errMsg += "消息: " + args.ErrorMessage + "
";
if (errorType == "ParserError") {
errMsg += "文件: " + args.xamlFile + "
";
errMsg += "行: " + args.lineNumber + "
";
errMsg += "位置: " + args.charPosition + "
";
}
else if (errorType == "RuntimeError") {
if (args.lineNumber != 0) {
errMsg += "行: " + args.lineNumber + "
";
errMsg += "位置: " + args.charPosition + "
";
}
errMsg += "方法名称: " + args.methodName + "
";
}
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; 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;
USERNAME= e.InitParams["USERNAME"];
USERID = e.InitParams["USERID"];
this.RootVisual = new BeginHome(USERNAME, USERID);//跳转到其他页面
}
第二种:
在Application_Startup方法中得到web程序的参数,具体代码如下:
private void Application_Startup(object sender, StartupEventArgs e){
IDictionary<String, String> paras = System.Windows.Browser.HtmlPage.Document.QueryString;
AppID = paras["APPID"];
strChartName = paras["ChartName"];
…………}