如何动态InitParamsn参数的输入

sl3中已经把过去的asp.net silverlight控制删除了,所以无法通过在aspx页里直接用silverlight控件在后台动态输入参数给xap文件。

解决办法如下:

1.在展示页中我使用了一个asp.net的Literal控件,此控件可以承载html代码。请注意<asp:Literal部分,代码如下:

 1:    <div id="silverlightControlHost"> 
 2:        <object data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="100%" height="100%"> 
 3:    <param name="source" value="ClientBin/SilverlightApplication18.xap"/> 
 4:    <param name="onError" value="onSilverlightError" /> 
 5:    <param name="background" value="white" /> 
 6:    <param name="minRuntimeVersion" value="3.0.40624.0" /> 
 7:    <param name="autoUpgrade" value="true" /> 
 8:  <asp:Literal ID="ParamInitParams" runat="server"></asp:Literal> 
 9:    <a href="http://go.microsoft.com/fwlink/?LinkID=149156&v=3.0.40624.0" style="text-decoration:none"> 
10:      <img src="http://go.microsoft.com/fwlink/?LinkId=108181" alt="Get Microsoft Silverlight" style="border-style:none"/> 
11:    </a> 
12:     </object><iframe id="_sl_historyFrame" style="visibility:hidden;height:0px;width:0px;border:0px"></iframe></div> 
13:    </div>

2.为了测试,我们必须在后台做一下输入InitParamsn参数的动作。这里我输入了一个名字id的参数,它的值为jac,代码如下:

1:  protected void Page_Load(object sender, EventArgs e)
2:  {
3:      if (!IsPostBack)
4:      {
5:          ParamInitParams.Text = 
String.Format(@"<param name=""InitParams"" value=""{0}"" />", "id=jac");
6:      }
7:  }

3.现在到sl里如何接收InitParams参数了,在silverlight项目里我们要在app.xaml.cs里定义前取得InitParams参数:

1:  internal IDictionary<string, string> getdata;
2:  private void Application_Startup(object sender, StartupEventArgs e)
3:  {
4:      this.RootVisual = new MainPage();
5:      getdata = e.InitParams;
6:  }

4.为了测试sl里是否正确接收到InitParams,我们在Mainpage.xaml.cs里作一下显示的动作:

 1:  public MainPage() 
 2:   { 
 3:       InitializeComponent(); 
 4:       Loaded += new RoutedEventHandler(MainPage_Loaded); 
 5:   }
 6:  
 7:   void MainPage_Loaded(object sender, RoutedEventArgs e) 
 8:   { 
 9:       App myapp = (App)App.Current; 
10:       MessageBox.Show(myapp.getdata.First().Key + ":" + myapp.getdata.First().Value); 
11:   }

你可能感兴趣的:(params)