天天看点

向silverlight传递自定义参数

在silverlight往往还是有一些获取不到的东西,比如说客户机的ip等的数据.可以通过初始化sl时把参数传入sl中.

1.修改page类

public Page(string passText)

{

    InitializeComponent();

    txtPass.Text = passText;

}

2.修改App.xaml.cs

 private void Application_Startup(object sender, StartupEventArgs e)

        {

            // Load the main control

            string passText = e.InitParams["passText"];

            this.RootVisual = new Page(passText);

        }

3.传递参数

方法1:

向silverlight传递自定义参数
向silverlight传递自定义参数

Code

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<%@ 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">

<head runat="server">

    <title>passtxt</title>

</head>

<body>

    <form id="form1" runat="server">

    <div>

        <asp:ScriptManager ID="ScriptManager1" runat="server">

        </asp:ScriptManager>

        <div style="height: 100%;">

            <asp:Silverlight ID="Xaml1" runat="server" Source="~/ClientBin/HowTo.xap" Version="2.0"

                Width="100%" Height="100%"/>

        </div>

    </div>

    </form>

</body>

</html>

    protected void Page_Load(object sender, EventArgs e)

    {

        Xaml1.InitParameters = "passText=参数_方法1";

    }

方法2:

        <object data="data:application/x-silverlight," type="application/x-silverlight-2-b1" InitParameters="passText=123" width="100%" height="100%">

            <param name="InitParams" value="passText=参数_方法2" />

            <param name="source" value="ClientBin/HowTo.xap"/>

            <param name="onerror" value="onSilverlightError" />

            <param name="background" value="white" />

            <a href="http://go.microsoft.com/fwlink/?LinkID=108182" style="text-decoration: none;">

                 <img src="http://go.microsoft.com/fwlink/?LinkId=108181" alt="Get Microsoft Silverlight" style="border-style: none"/>

            </a>

        </object>

传递多个参数也是一样的.定义的时候改一下.后边的方法也类似.

public Page(string passText1,string passText2)

    txtPass1.Text = passText1;

    txtPass2.Text = passText2;

参数间用","隔开

<param name="InitParams" value="passText1=参数1,passText2=参数2" />

ok 获取参数完成 ;)

附录:获取客户机ip   "System.Net.Dns.GetHostAddresses(System.Net.Dns.GetHostName()).GetValue(0).ToString();"

作者:nasa

联系:[email protected]

QQ:12446006