silverlight/xap如何接收参数?
silverlight不能象flash一样,直接以类似xxx.xap?name=123的方式传递参数,而是要用另一种方式实现:
html部分(注意高亮部分):
<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:
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部分:
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();
}
}
注:这里,我们重载了一个带参数的构造函数用于接受参数
出处:http://yjmyzz.cnblogs.com
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。