Silverlight与Asp.net之间的调用及参数传递

共有两种方式来传递初始化参数

  1)在html或者aspx页面中object对象中加入一下代码

  参数格式:参数名 = 值,参数名 = 值,...

<param name="initParams" value = "name = John , sex = m"/>

  取得参数的方法

  在app页面中

StringBuilder txtMsg = new StringBuilder();
foreach (string key in e.InitParams.Keys)
{
txtMsg.Append(String.Format("{0} : {1}", key, e.InitParams[key])).Append("\r");
}
MessageBox.Show(txtMsg.ToString());

  在任意页面中

StringBuilder txtMsg = new StringBuilder();
foreach (string key in App.Current.Host.InitParams.Keys)
{
txtMsg.Append(String.Format("{0} : {1}", key, App.Current.Host.InitParams[key])).Append("\r");
}
MessageBox.Show(txtMsg.ToString());

  2)在URL中传递参数也可以用表单传递参数

http://localhost:45876/Silverlight4TestPage.aspx?name=Cena&age=100

在App页面中

IDictionary<string, string> queryString = HtmlPage.Document.QueryString;

if (queryString != null && queryString.Count > 0 && queryString.ContainsKey("name"))

{

string str = queryString["name "].ToString());

}

  在任意页中取参数值

txtMsg = new StringBuilder();
foreach (string key in HtmlPage.Document.QueryString.Keys)
{
txtMsg.Append(String.Format("{0} : {1}", key, HtmlPage.Document.QueryString[key])).Append("\r");
}
if (!"".Equals(txtMsg.ToString()))
{
MessageBox.Show(txtMsg.ToString());
}

  另外:

<param name="initParams" value = "age = 24" />
<param name="initParams" value = "name = John , sex = m"/>

  在IE和谷歌浏览器中解析的顺序不一样,IE是第一条参数而谷歌是第二条参数

但是无论哪种浏览器都只一条记录

  3)在Silverlight中打开Asp.net页面或Silverlight页面并传值

string targetFeatures = string.Format("top=0,left=0,width={0},height={1},menubar=0,toolbar=0,directories=0,location=0,status=0,scrollbars=0,resizable=1", w, h);

string uri = string.Format("http://localhost:1452/Be.BPM.Module.FormTestPage.aspx?auditflow=0");

HtmlPage.Window.Navigate(new Uri(uri, UriKind.RelativeOrAbsolute), "_blank", targetFeatures);

HtmlPopupWindowOptions options = new HtmlPopupWindowOptions();
options.Width = 500;
options.Height = 500;
Uri uri = new Uri("http://localhost:3150/WebForm1.aspx?name=lyf", UriKind.Absolute);
HtmlWindow win = HtmlPage.PopupWindow(uri, "_blank", options);

  本文部分内容来自小鬼大周的博客,原文地址:http://www.cnblogs.com/Zyang/archive/2012/01/11/InitParams.html

posted @ 2014-01-24 14:37  liuyunfeng  阅读(156)  评论(0编辑  收藏  举报