一个新建的silverlight项目在解决方案中往往有两个项目,silverlight的本身项目和供其在浏览器中展现的Web项目,在web项目中有相应的web页面提供给silverlight程序承载展现,那么如何将web页面的数据(比如系统所需的一些配置)传递到silverlight的程序中呢?在App.xaml.cs中的Application_Startup函数的参数StartupEventArgs的InitParams 属性为我们提供了这样的一个实现。
一. 新建silverlight项目如下:
将web项目设为启动项,InitParamsTestPage.html设为起始页。
二. InitParamsTestPage.html代码(修改部分)
<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/InitParams.xap"/> <param name="onError" value="onSilverlightError" /> <param name="background" value="white" /> <param name="minRuntimeVersion" value="5.0.61118.0" /> <param name="autoUpgrade" value="true" /> <!-- 利用initParams属性添加了三个参数--> <param name="initParams" value="IP=127.0.0.1,Port=8989,ServiceName=testName" /> <a href="http://go.microsoft.com/fwlink/?LinkID=149156&v=5.0.61118.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>
在上面的代码中,我们想initParams属性中添加了IP, Port,ServiceName这三个参数,他们之前用逗号”,“分隔。
三. 在App.xaml.cs中获取上面的参数并显示
private void Application_Startup(object sender, StartupEventArgs e) { MainPage mP = new MainPage(); mP.IpLab.Content = e.InitParams["IP"]; mP.PortLab.Content = e.InitParams["Port"]; mP.WsNameLab.Content = e.InitParams["ServiceName"]; this.RootVisual = mP; }
可以看到,通过StartupEventArgs的InitParams属性来传递的,实际上InitParams是一个Dictionary<string, string>类型的,所以可以直接使用 [] 获取。
MainPage.xaml的显示部分代码:
<UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk" x:Class="InitParams.MainPage" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="400"> <Grid x:Name="LayoutRoot" Background="White"> <sdk:Label Content="IP:" HorizontalAlignment="Left" Height="20" Margin="79,75,0,0" VerticalAlignment="Top" Width="28"/> <sdk:Label Name="IpLab" Content="" HorizontalAlignment="Left" Height="20" Margin="133,75,0,0" VerticalAlignment="Top" Width="142"/> <sdk:Label Content="Port:" HorizontalAlignment="Left" Height="20" Margin="79,109,0,0" VerticalAlignment="Top" Width="28"/> <sdk:Label Name="PortLab" Content="" HorizontalAlignment="Left" Height="20" Margin="133,109,0,0" VerticalAlignment="Top" Width="142"/> <sdk:Label Content="Name:" HorizontalAlignment="Left" Height="20" Margin="79,147,0,0" VerticalAlignment="Top" Width="41" RenderTransformOrigin="0.832,2.653"/> <sdk:Label Name="WsNameLab" Content="" HorizontalAlignment="Left" Height="20" Margin="133,147,0,0" VerticalAlignment="Top" Width="142" RenderTransformOrigin="0.832,2.653"/> </Grid> </UserControl>
四. 效果
可以看到成功获取到我们在InitParamsTestPage.html页面中配置好的参数。
五 . 扩展
我们在项目开发中往往将一些经常改变的参数配置在一个配置文件中,这个方便统一管理,所以往往我们的配置数据都是从配置文件中获取的,这时我们就可以使用javascript来实现读取我们的配置文件中的数据并使用html元素的innerHTML属性来动态设置参数;
InitParamsTestPage.html的代码修改为:
<!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> <title>InitParams</title> <style type="text/css"> html, body { height: 100%; overflow: auto; } body { padding: 0; margin: 0; } #silverlightControlHost { height: 100%; text-align:center; } </style> <script type="text/javascript" src="Silverlight.js"></script> <script type="text/javascript"> function onSilverlightError(sender, args) { var appSource = ""; if (sender != null && sender != 0) { appSource = sender.getHost().Source; } var errorType = args.ErrorType; var iErrorCode = args.ErrorCode; if (errorType == "ImageError" || errorType == "MediaError") { return; } var errMsg = "Silverlight 应用程序中未处理的错误 " + appSource + "\n" ; errMsg += "代码: "+ iErrorCode + " \n"; errMsg += "类别: " + errorType + " \n"; errMsg += "消息: " + args.ErrorMessage + " \n"; if (errorType == "ParserError") { errMsg += "文件: " + args.xamlFile + " \n"; errMsg += "行: " + args.lineNumber + " \n"; errMsg += "位置: " + args.charPosition + " \n"; } else if (errorType == "RuntimeError") { if (args.lineNumber != 0) { errMsg += "行: " + args.lineNumber + " \n"; errMsg += "位置: " + args.charPosition + " \n"; } errMsg += "方法名称: " + args.methodName + " \n"; } throw new Error(errMsg); } function WindOnLoad() { var xmlDoc; var initParamsString = ''; var xmlDoc = new ActiveXObject("Microsoft.XMLDOM"); xmlDoc.async = false; xmlDoc.load("ConfigParams.xml"); if (xmlDoc == null) { alert('您的浏览器不支持xml文件读取,于是本页面禁止您的操作,推荐使用IE5.0以上可以解决此问题!'); window.location.href = '/Index.aspx'; return; } var nodes = xmlDoc.getElementsByTagName("p"); for (var i = 0; i < nodes.length; i++) { initParamsString += nodes[i].text; if (i != nodes.length) { initParamsString += ","; } } SetInitParams(initParamsString); } function SetInitParams(initParamsString) { var silverlightControlHtml = ' <object id="silverlightControl" data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="100%" height="100%">'; silverlightControlHtml += '<param name="source" value="ClientBin/InitParams.xap" />'; silverlightControlHtml += '<param name="onerror" value="onSilverlightError" />'; silverlightControlHtml += '<param name="minRuntimeVersion" value="5.0.61118.0" />'; silverlightControlHtml += '<param id="InitParams" name="InitParams" value="' + initParamsString + '"/>'; silverlightControlHtml += '</object>'; silverlightControlHtml += '<iframe id="_sl_historyFrame" style="visibility: hidden; height: 0; width: 0; border: 0px"></iframe>'; document.getElementById("silverlightControlHost").innerHTML = silverlightControlHtml; } window.onload = WindOnLoad; </script> </head> <body> <div id="silverlightControlHost"> </div> </body> </html>
可以看到,我们添加了两个javascript函数WindOnLoad,和SetInitParams分别用来读取配置文件和动态填写html代码,最终的效果和上面的一样。
然后需要添加一个配置文件ConfigParams.xml:
<?xml version="1.0" encoding="utf-8" ?> <Params> <p>IP=127.0.0.1</p> <p>Port=8989</p> <p>ServiceName=testName</p> </Params>
运行效果: