经典论坛 » Silverlight 专版 » How to pass initialization params to a Silverlight 2 app
阿森纳
银牌会员
专长 C#,MSSQL,SL
This is something of an FAQ on our internal discussion lists, so I figured it merited a post. Step 1: Declare the initParams param on the Silverlight plug-in and pass in a comma-delimited set of key-value pair tokens. For e.g. "key1=value1,key2=value2,key3=value3".复制内容到剪贴板代码:<object type="application/x-silverlight" width="100%" height="100%"> <param name="source" value="ClientBin/SLInitParams.xap"/> <!-- startPage key can have values Page1 or Page2 --> <param name="initParams" value="startPage=Page1" /> </object>For best results, the key and value tokens in the initParams string must be restricted to alphanumeric values. There currently isn't support for escaping equality signs or commas, for instance, should they exist in the key or value part of the token. Step 2: In the Application's Startup event handler, extract the initialization parameters via StartupEventArgs.InitParams property. You'll get an IDictionary<string, string>. // Contents of App.xaml.cs复制内容到剪贴板代码:private void Application_Startup(object sender, StartupEventArgs e) { string startPage = e.InitParams["startPage"]; if (startPage != null && startPage.Equals("Page1")) { // Load Page1 this.RootVisual = new Page(); } else { // Load Page2 this.RootVisual = new AlternatePage(); } }Simple, huh? I have a sample project on how to pass initialization params to a Silverlight 2 app, and conditionally show UI, up on my sky drive.
<object type="application/x-silverlight" width="100%" height="100%"> <param name="source" value="ClientBin/SLInitParams.xap"/> <!-- startPage key can have values Page1 or Page2 --> <param name="initParams" value="startPage=Page1" /> </object>
private void Application_Startup(object sender, StartupEventArgs e) { string startPage = e.InitParams["startPage"]; if (startPage != null && startPage.Equals("Page1")) { // Load Page1 this.RootVisual = new Page(); } else { // Load Page2 this.RootVisual = new AlternatePage(); } }
查看详细资料
TOP
版主
专长 .NET,AS,SL
查看个人网站
冥隼
原帖由 lbx1979 于 2008-3-11 14:18 发表 我不知道该不该转帖别人的blog文章, 因为可能都看过了, 只是觉得这个板块关注的人较少, 增加点人气