通过后面的代码将silverlight加载到aspx页面

时间:2012-01-14 00:27:32

标签: c# asp.net .net silverlight

我需要在该页面上的按钮上加载一个aspx页面的一部分中的silverlight应用程序。某些init参数需要根据按钮单击时主机页面上的用户输入传递给silverlight应用程序。怎么做?

我认为我需要从代码隐藏创建silverlight对象来设置自定义InitParameters。知道怎么做吗?

1 个答案:

答案 0 :(得分:3)

扩展到提到的here,您可以这样做:

HtmlGenericControl myHtmlObject = new HtmlGenericControl("object");
myHtmlObject.Attributes["data"] = "data:application/x-silverlight-2";
myHtmlObject.Attributes["type"] = "application/x-silverlight-2";
myHtmlObject.Attributes["width"] = "100%";
myHtmlObject.Attributes["height"] = "100%";
this.Page.Controls.Add(myHtmlObject);

HtmlGenericControl mySourceParam = new HtmlGenericControl("param");
mySourceParam.Attributes["name"] = "source";
mySourceParam.Attributes["value"] = "ClientBin/MySilverlightApplication.xap";
myHtmlObject.Controls.Add(mySourceParam);

HtmlGenericControl myOnErrorParam = new HtmlGenericControl("param");
myOnErrorParam .Attributes["name"] = "onError";
myOnErrorParam .Attributes["value"] = "onSilverlightError";
myHtmlObject.Controls.Add(myOnErrorParam);

HtmlGenericControl myInputParam = new HtmlGenericControl("param");
myOnErrorParam .Attributes["name"] = "InitParameters";
myOnErrorParam .Attributes["value"] = "param1=Hello,param2=World";
myHtmlObject.Controls.Add(myInputParam);

this.Page.Controls.Add(myHtmlObject);