vs自动生成Silverlight加载页面,安装和升级都会自动连接到微软网站上下载更新。
但一些企业级应用中,客户端不能访问外网,就导致Silverlight技术应用有缺陷。
一般的解决方案是,将silverlight安装程序上传到服务器上,在Html中添加该地址,代码如下:
<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/Map.xap"/>
<param name="onError" value="onSilverlightError" />
<param name="background" value="white" />
<param name="minRuntimeVersion" value="4.0.50826.0" />
<param name="autoUpgrade" value="true" />
<a href="http://localhost/Silverlight.exe" style="text-decoration:none">
<img src="http://localhost/SLMedallion_CHS.png" 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>
此方案对于没有安装过的客户端有效,但如果客户端已经安装过Silverlight,而开发的Silverlight版本较高,即minRuntimeVersion比客户端的版本高,则此时客户端仍会自动连接到微软网站去升级。
最好的解决方案应该是使用silverlight.js去加载Silverlight,代码如下:
<script type="text/javascript" src="Silverlight.js"></script>
<script type="text/javascript">
window.onload = function() {
silverlightControlHost.innerHTML = embedSilverlight(null, "sl", "");
}
function embedSilverlight(parentElement, pluginId, userContext) {
var getSilverlightMethodCall = "http://localhost/Silverlight.exe"
var installImageUrl = "http://localhost/SLMedallion_CHS.png";
var imageAltText = "Get Microsoft Silverlight";
var altHtml =
"<a href='{1}' style='text-decoration: none;'>" +
"<img src='{2}' alt='{3}' " +
"style='border-style: none'/></a>";
altHtml = altHtml.replace('{1}', getSilverlightMethodCall);
altHtml = altHtml.replace('{2}', installImageUrl);
altHtml = altHtml.replace('{3}', imageAltText);
return Silverlight.createObjectEx({
source: "ClientBin/Map.xap",
id: "sl",
properties: {
width: "100%",
height: "100%",
background: "white",
alt: altHtml,
version: "5.0.61118.0",
autoUpgrade:false },
events: {
onError: onSLError,
onLoad: onSLLoad },
enableGPUAcceleration: false
});
}
function onSLLoad(plugIn, userContext, sender) {
window.status +=
plugIn.id + " loaded into " + userContext + ". ";
}
function onSLError(sender, args) {
// Display error message.
alert(args);
}
</script>
<form id="form1" runat="server" style="height: 100%">
<div id="silverlightControlHost"></div>
<iframe id="_sl_historyFrame" style="visibility:hidden;height:0px;width:0px;border:0px"></iframe>
</form>
要注意的是autoUpgrade要设置为false.
解决这个问题搞了好久,希望对你有用。