silverlight不能象flash一样,直接以类似xxx.xap?name=123的方式传递参数,而是要用另一种方式实现:
html部分(注意高亮部分):
代码语言:js复制<object data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="100%" height="100%">
<param name="source" value="ClientBin/InitParams.xap?color=red"/>
<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="name=jimmy.yang,age=30" />
<a href="http://go.microsoft.com/fwlink/?LinkID=149156&v=3.0.40624.0" style="text-decoration: none;">
<img src="http://go.microsoft.com/fwlink/?LinkId=108181" alt="获取 Microsoft Silverlight" style="border-style: none"/>
</a>
</object>
注:InitParams传递参数时,必须以x=y,a=b这样的键值对格式传入
cs部分:
App.Xaml.cs:
代码语言:js复制public partial class App : Application
{
IDictionary<string, string> _dic;
...
private void Application_Startup(object sender, StartupEventArgs e)
{
_dic = e.InitParams;
this.RootVisual = new MainPage(_dic);
}
...
注:这里用e.InitParams取得传入的参数,同时利用new MainPage(_dic)将参数传到需要的xaml中去
MainPage.Xaml.cs部分:
代码语言:js复制public partial class MainPage : UserControl
{
IDictionary<string, string> _params;
public MainPage()
{
InitializeComponent();
}
public MainPage(IDictionary<string, string> dicParam)
{
InitializeComponent();
_params = dicParam;
StringBuilder sb = new StringBuilder();
foreach (var item in _params)
{
sb.Append(string.Format("key:{0} , value:{1}" Environment.NewLine, item.Key, item.Value));
}
txtParam.Text = sb.ToString();
}
}
注:这里,我们重载了一个带参数的构造函数用于接受参数