Recently, some people have been asking about how to pass parameters between xap and HTML or Asp.net. Next we will learn how to pass this parameter.
First, create a project htmltsilverlight and put a textblock on mainpage to display the parameters passed by the HTML or aspx file;
Next, add the required parameters to htmltsilverlighttestpage.aspx20.htmltsilverlighttestpage.html for the webobject. Generally, there are many server paths passed here. Here we will pass a server path and put a textblock on the mainpage.
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; }
Next, modify the htmltsilverlighttestpage.html file in the webproject and add the passing parameters. Htmltsilverlighttestpage. The usage of aspx is the same.
<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 "> </a> </Object> <IFRAME id =" _ sl_historyframe "style =" visibility: hidden; Height: 0px; width: 0px; Border: 0px "> </iframe> </div> </form> </body>
We added a serverurl = http: // 127.0.0.1/service. aspx parameter, which is obtained in mainpage.
We add a static attribute to the app. XAML. CS file for access by other classes. In the parameter startupeventargs of the application_startup event, the Code is as follows:
/// <Summary >/// server path transmitted by HTML or aspx /// </Summary> Public static string serverurl {Get; set ;} private void application_startup (Object sender, startupeventargs e) {// exception handling is required to obtain the parameter ---- serverurl = e if it cannot be obtained. initparams ["serverurl"]. tostring (); this. rootvisual = new mainpage ();}
Finally, set the webproject as the startup project, and htmltsilverlighttestpage.html as the start page. F5 will see the following results. The parameters are retrieved and displayed on the textblock. You can add multiple parameters. Please try them by yourself. Instance code download: http://files.cnblogs.com/Clivia/HtmlTSilverlight.rar