一.【使用html将silverlight添加到网页】
当我新建立一个silverlight 项目的时候,我们会在.web项目当中发现对应 silverlight应用程序名称的.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>MySilverlightDemo</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 onSourceDownloadProgressChanged(sender, eventArgs) {
sender.findName("uxStatus").SetValue("Foreground", "White");
sender.findName("uxStatus").Text = "Loading: " + Math.round((eventArgs.progress * 1000)) / 10 + "%";
sender.findName("uxProgressBar").ScaleY = eventArgs.progress * 356;
}
</script>
</head>
<body>
<div id="silverlightControlHost">
<object data="data:application/x-silverlight-2," type="application/x-silverlight-2"width="100%" height="100%">
<param name="source" value="ClientBin/MySilverlightDemo.xap" />
<param name="onerror" value="onSilverlightError" />
<param name="background" value="white" />
<param name="minRuntimeVersion" value="3.0.40818.0" />
<param name="autoUpgrade" value="true" />
<param name="splashscreensource" value="SplashScreen.xaml" />
<param name="onSourceDownloadProgressChanged" value="onSourceDownloadProgressChanged" />
<a href="http://go.microsoft.com/fwlink/?LinkID=149156&v=3.0.40818.0" style="text-decoration: none;">
<img src="http://go.microsoft.com/fwlink/?LinkId=108181" alt="获取 Microsoft Silverlight"
style="border-style: none" />
</a>
</object>
<iframe id="_sl_historyFrame" style='visibility: hidden; height: 0; 0; border: 0px'>
</iframe>
</div>
</body>
</html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>MySilverlightDemo</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 onSourceDownloadProgressChanged(sender, eventArgs) {
sender.findName("uxStatus").SetValue("Foreground", "White");
sender.findName("uxStatus").Text = "Loading: " + Math.round((eventArgs.progress * 1000)) / 10 + "%";
sender.findName("uxProgressBar").ScaleY = eventArgs.progress * 356;
}
</script>
</head>
<body>
<div id="silverlightControlHost">
<object data="data:application/x-silverlight-2," type="application/x-silverlight-2"width="100%" height="100%">
<param name="source" value="ClientBin/MySilverlightDemo.xap" />
<param name="onerror" value="onSilverlightError" />
<param name="background" value="white" />
<param name="minRuntimeVersion" value="3.0.40818.0" />
<param name="autoUpgrade" value="true" />
<param name="splashscreensource" value="SplashScreen.xaml" />
<param name="onSourceDownloadProgressChanged" value="onSourceDownloadProgressChanged" />
<a href="http://go.microsoft.com/fwlink/?LinkID=149156&v=3.0.40818.0" style="text-decoration: none;">
<img src="http://go.microsoft.com/fwlink/?LinkId=108181" alt="获取 Microsoft Silverlight"
style="border-style: none" />
</a>
</object>
<iframe id="_sl_historyFrame" style='visibility: hidden; height: 0; 0; border: 0px'>
</iframe>
</div>
</body>
</html>
我们可以清晰的看到,这里面有有脚本,哪个能帮助我们家在silverlight程序呢,就是通过这个object对象来实现的。
二.【使用javascript将silverlight 嵌入到网页当中】
代码
<script type="text/javascript">
Silverlight.createObject(
"ClientBin/SilverlightApplication1.xap", // source
silverlightControlHost, // parent element
"slPlugin", // id for generated object element
{
"100%", height: "100%", background: "white",
version:"3.0.40620.0"
},
{ onError: onSLError, onLoad: onSLLoad },
"param1=value1,param2=value2",
"context" // context helper for onLoad handler.
);
</script>
Silverlight.createObject(
"ClientBin/SilverlightApplication1.xap", // source
silverlightControlHost, // parent element
"slPlugin", // id for generated object element
{
"100%", height: "100%", background: "white",
version:"3.0.40620.0"
},
{ onError: onSLError, onLoad: onSLLoad },
"param1=value1,param2=value2",
"context" // context helper for onLoad handler.
);
</script>
我就不太喜欢用这种方式,主要是js水平有限啊,再加上提示少.
三.【指定和检索自定义参数】
这个可是很重要的,不过也很简单。
在承载页面<object>下面加上 <param name="initParams" value="A=sa,B=sa"/>
在silverlight 应用程序启动项里面
if (e.InitParams.Count > 0)
{
switch (e.InitParams["A"].ToString())
{
case"A":
System.Windows.Browser.HtmlPage.Window.Alert("你好啊");
break;
case"":
break;
}
}
这样我们就可以根据这个来指定我们每个页面的操作喽.