SilverLight学习笔记--Silverligh之如何把InitParams 初始化参数从WebPage传递到Silverlight

  今天学习内容是,我们将利用Silverlight给我们提供的一个便利的方法来实现: 当一个web page加裁时,把指定参数(或信息)从 web page传递到silverlight中,这就是initParams。
  我们可以利用它把诸如页面url等相关信息传递到silverlight中(当然也可以传递其它信息)。
  initParams 信息是按照 string/value对的方式来存放的。我们将学习如何设置以及如何读取它们。下面开始我们的实验。
  仍按惯例,新建一个Silverlight应用程序,命名为:SLInitParamsFromWbToSL。如图:


一、在Web Page页面上放置我们将要传递的InitParams信息(InitParams信息设置格式与放置位置)。
WebPage页面是放置我们Silverlight控件的Host页面(本例为SLInitParamsFromWbToSLTestPage.aspx页面内容),其代码是:
 <%@ Page Language="C#" AutoEventWireup="true" %>
  
<%@ Register Assembly="System.Web.Silverlight" Namespace="System.Web.UI.SilverlightControls"
    TagPrefix
="asp" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" style="height:100%;">
<head runat="server">
    
<title>SLInitParamsFromWbToSL</title>
</head>
<body style="height:100%;margin:0;">
    
<form id="form1" runat="server" style="height:100%;">
        
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
        
<div  style="height:100%;">
         
<asp:Silverlight ID="Xaml1" runat="server" Source="~/ClientBin/SLInitParamsFromWbToSL.xap" MinimumVersion="2.0.31005.0" Width="100%" Height="100%"  InitParameters="Australia=Mebourne,China=ChengDu,USA=Washington"/>
        
</div>
    
</form>
</body>
</html>  
其间我们可以看到有一个Siverlight控件<asp:Silverlight ID="Xaml1" />,我们的InitParams信息将放置在其中,因为它有一个InitParams参数,设置格式为:
 InitParameters="Australia=Mebourne,China=ChengDu,USA=Washington"
这就是将要传递到Silverlight中的InitParams 键值对信息.

 二、在Silverlight中读取我们传递过来的InitParams信息。
   当上面的页面(SLInitParamsFromWbToSLTestPage.aspx)加裁时,InitParameters的信息将会传递到Silverlight应用程序中,也即:传递到Silverlight的 App中,那么,我们如何在Silverlight application中获取InitParameters的信息呢?
    我们知道,当我们新建一个Silverlight application时, Visual Studio 会为我们自动创建四个文件: App.xaml, App.xaml.cs, Page.xaml, and Page.xaml.cs. 通常,我们只关注后面两个文件,但当我们想要获取initParams时,我们就必须要关注App.xaml与App.xaml.cs文件了。
    App.xaml.cs文件的内容如下:
  public partial class App : Application
    {
        
public App()
        {
            
this.Startup += this.Application_Startup;
            
this.Exit += this.Application_Exit;
            
this.UnhandledException += this.Application_UnhandledException;

            InitializeComponent();
        }

        
private void Application_Startup(object sender, StartupEventArgs e)
        {
            
this.RootVisual = new Page();
        }

        
private void Application_Exit(object sender, EventArgs e)
        {

        }
       
private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
        {   }
        
private void ReportErrorToDOM(ApplicationUnhandledExceptionEventArgs e)
        {}
}
     在此文件中,我们要特别关注Application_Startup 方法,它是在当Silverlight应用程序Startup事件触发时开始执行。其StartupEventArgs传入参数中含有一个名为InitParams的属性,其所含信息就是我们在第一步骤时设置的信息。而且,目前我们只有这么一个途径来取得InitParams中的信息。
    下面我们有两个途径来实现在Page.xaml.cs后台代码中访问到此处的InitParams信息。
     (一)途径一:通过在App.xaml.cs中添加一个属性来实现InitParameters信息的可访问性。。
       1、添加属性: MyInitParams ,代码如下:
   public System.Collections.Generic.IDictionary  <string,string > MyInitParams { get;set;}
       2、在Application_Startup 方法中给属性MyInitParams 赋值
   private void Application_Startup(object sender, StartupEventArgs e)
        {
            
this.RootVisual = new Page();
            
//在这个位置,我们可以找到 e.InitParams,它就是这个Silverlight程序的初始化参数
            this.MyInitParams = e.InitParams; //在此处给我们上面建立的MyInitParams属性赋值
        }
        经过上面两步修改后的App.xaml.cs代码如下:
Code
  3、在Page.xaml.cs中访问MyInitParams信息。
   i、按给定的Key访问Value
  this.txtBxKey.Text = "澳大利亚的城市有: " + myApp.MyInitParams["Australia"];
  ii、遍历InitParams的内容
            foreach (string key in myApp.MyInitParams.Keys)
            {
                
this.txtBxValue.Text += key + "" + myApp.MyInitParams[key] + "\n";
            }
      Page.xaml.cs代码如下:
Code
  (二)途径二:通过改造Page.xaml.cs的Page()构造函数来实现InitParameters信息的可访问性。
  1、在App.xaml.cs代码的Application_Startup方法中修改代码如下(把e.InitParams做为参数传递到Page构造函数中):
this.RootVisual = new Page(e.InitParams);
  2、修改Page.xaml.cs中的Page()构造函数,使其带有一个传入参数
  public Page(IDictionary<stringstring> initParams)
        {
            InitializeComponent();
        }
 Page.xaml.cs全部代码如下:
Code
 程序执行的效果如图:


前往:Silverlight学习笔记清单

本文程序在Silverlight2.0和VS2008环境中调试通过。本文参照了部分网络资料,希望能够抛砖引玉,大家共同学习。
(转载本文请注明出处)

posted @ 2009-08-19 09:43  wsdj  阅读(2767)  评论(0编辑  收藏  举报