Silverlight Xap和Html Asp.net参数传递

近来总是有朋友在询问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上了。
2010112420552478.jpg
这个参数可以添加多个,请同学们自己尝试。

转载于:https://www.cnblogs.com/Clivia/archive/2010/11/24/silverlight08.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值