对Silverlight如何进入页面感到困惑

时间:2010-11-12 00:47:54

标签: asp.net-mvc silverlight

我花了一些时间浏览http://www.silverlight.net/learn/quickstarts/

上的所有示例

我仍然对Silverlight很失落。我不明白它是如何“进入”网站的。就像...是否有任何类型的教程显示如何创建一个HTML网页来检索Silverlight页面并显示它,以便您可以使用它?

到目前为止我所有必须使用的是默认生成的.aspx文件,这些文件并没有真正告诉我。甚至使用Visual Studio中的默认“MVC应用程序”生成器也是一种混乱和混乱。

我已经完成了一些谷歌搜索并浏览了一些参考资料,但也许我只是有点愚蠢。我只是没有得到所有“聚集在一起”的地方,可以这么说。任何提示?或者我只是在学习之外?

3 个答案:

答案 0 :(得分:6)

简短的回答是,您通过在HTML中使用对象标记将Silverlight应用程序包含在网页中。然后,Web浏览器负责加载silverlight应用程序,类似于加载Flash应用程序的方式。查看这些quickstarts以获取更详细的说明。不要气馁,这对很多开发人员来说都是一个新概念:)。

以下是嵌入网页的Silverlight应用程序示例:

<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="HelloWorld.xap"/>
          <param name="onError" value="onSilverlightError" />
          <param name="background" value="white" />
          <param name="minRuntimeVersion" value="4.0.50401.0" />
          <param name="autoUpgrade" value="true" />
          <a href="http://go.microsoft.com/fwlink/?LinkID=149156&v=4.0.50401.0" 
                   style="text-decoration:none">
          <img src="http://go.microsoft.com/fwlink/?LinkId=161376" 
                   alt="Get 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>

答案 1 :(得分:2)

只需创建一个Silverlight应用程序并选择它来在Web站点中创建一个Silverlight应用程序。

它将创建一个项目,生成包含silverlight对象的页面,该对象在客户端上加载xap文件。

xap文件位于ClientBin中,xap文件是Silverlight应用程序。

只要将对象源<param>指向xap文件的路径,它就应该加载相应的Silverlight应用程序。

alt text

它创建的项目:

alt text

示例aspx页面:

<%@ Page Language="C#" AutoEventWireup="true" %>

<!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>SilverlightApplication1</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 = "Unhandled Error in Silverlight Application " +  appSource + "\n" ;

            errMsg += "Code: "+ iErrorCode + "    \n";
            errMsg += "Category: " + errorType + "       \n";
            errMsg += "Message: " + args.ErrorMessage + "     \n";

            if (errorType == "ParserError") {
                errMsg += "File: " + args.xamlFile + "     \n";
                errMsg += "Line: " + args.lineNumber + "     \n";
                errMsg += "Position: " + args.charPosition + "     \n";
            }
            else if (errorType == "RuntimeError") {           
                if (args.lineNumber != 0) {
                    errMsg += "Line: " + args.lineNumber + "     \n";
                    errMsg += "Position: " +  args.charPosition + "     \n";
                }
                errMsg += "MethodName: " + args.methodName + "     \n";
            }

            throw new Error(errMsg);
        }
    </script>
</head>
<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/SilverlightApplication1.xap"/>
          <param name="onError" value="onSilverlightError" />
          <param name="background" value="white" />
          <param name="minRuntimeVersion" value="3.0.40818.0" />
          <param name="autoUpgrade" value="true" />
          <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=161376" alt="Get 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>
</html>

答案 2 :(得分:2)

Here是关于将Silverlight嵌入HTML的Microsoft教程。 .xap相当于现代applet的Jnlp和Flash的.swf。参数可以控制代码,也可以通过Javascript更新。浏览器中的silverlight插件执行应用程序代码。 HTML只是将其保留到位。