现在的位置: 首页 > 综合 > 正文

Silverlight Xap和Html Asp.net参数传递

2012年09月13日 ⁄ 综合 ⁄ 共 2017字 ⁄ 字号 评论关闭

近来总是有朋友在询问Xap和Html或者asp.net之间怎么传递参数,下面我们来学习哈这个参数传递的过程。

首先建一个项目HtmlTSilverlight,在MainPage上放一个TextBlock来显示Html或者Aspx文件传递过来的参数;

接下来,在web项目的HtmlTSilverlightTestPage.aspx或者HtmlTSilverlightTestPage.html中添加需要使用的参数,一般这个地方传递服务器路径的较多,我们在此就传递一个服务器路径显示在MainPage上放一个TextBlock上。

 
Xaml:

    <Grid x:Name="LayoutRoot" Background="White">
        <TextBlock Height="47" HorizontalAlignment="Left" Margin="52,49,0,0" x:Name="textBlock1" VerticalAlignment="Top" Width="311" />
    </Grid>
.cs

 public MainPage()
        {
            InitializeComponent();
            this.textBlock1.Text = App.ServerUrl;
        }

再其次,我们修改Web项目中的HtmlTSilverlightTestPage.html文件,添加传递参数。HtmlTSilverlightTestPage.aspx用法相同就再多解释。

<body>
    <form id="form1" runat="server" style="height:100%">
    <div id="silverlightControlHost">
        <object data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="100%" height="100%">
		  <param name="source" value="ClientBin/HtmlTSilverlight.xap"/>
		  <param name="onError" value="onSilverlightError" />
		  <param name="background" value="white" />
		  <param name="minRuntimeVersion" value="4.0.50826.0" />
		  <param name="autoUpgrade" value="true" />
          <param name="InitParams" value="ServerUrl=http://127.0.0.1/Service.aspx" />
		  <a href="http://go.microsoft.com/fwlink/?LinkID=149156&v=4.0.50826.0" style="text-decoration:none">
 			  <img src="http://go.microsoft.com/fwlink/?LinkId=161376" alt="获取 Microsoft Silverlight" style="border-style:none"/>
		  </a>
	    </object><iframe id="_sl_historyFrame" style="visibility:hidden;height:0px;width:0px;border:0px"></iframe></div>
    </form>
</body>

我们添加了一个ServerUrl=http://127.0.0.1/Service.aspx的参数,接下来就是在MainPage里面取到这个参数。

我们在APP.xaml.cs文件里添加一个静态属性,供其他的类访问,在Application_Startup事件的参数StartupEventArgs中取出这个参数,代码如下:

/// <summary>
        /// Html或者Aspx传递过来的服务器路径
        /// </summary>
        public static string ServerUrl { get; set; }


 private void Application_Startup(object sender, StartupEventArgs e)
        {
            //取得参数 需要异常处理----如果取不到的时候
            ServerUrl = e.InitParams["ServerUrl"].ToString();
            this.RootVisual = new MainPage();
        }
最后,设置web项目为启动项目,HtmlTSilverlightTestPage.html为起始页,F5就看到如下效果,参数被我们取出显示在了TextBlock上了。
这个参数可以添加多个,请同学们自己尝试。
【上篇】
【下篇】

抱歉!评论已关闭.