技术开发 频道

VS2010实践:在云中运行Silverlight应用

  接下来的对话框将会要求我们提供创建好的ASP.NET MVC应用程序项目,如图5所示,点击确定后,Silverlight项目就添加到总的解决方案中了。

 网页中嵌入适当的Silverlight对象

  图 5 新建Silverlight项目设置对话框

  下一步是在网页中嵌入适当的Silverlight对象,首先打开Veiws/Shared/ location下ASP.NET MVC 2项目的Site.Master文件,在部分添加下面的代码片段:

<head runat="server">
    
<title>
        
<asp:ContentPlaceHolder ID="TitleContent" runat="server" />
    
</title>
    
<link href="../../Content/Site.css" rel="stylesheet" type="text/css" />
    
<asp:ContentPlaceHolder ID="HeaderContent" runat="server" />
</head>

 

  我通常将其放于header的末尾部分,这样总是居于顶部,看起来顺眼些。

  接下来打开位于Views/Home/ directory下ASP.NET MVC 2项目的Index.aspx文件,添加下面的代码:

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    Home Page
</asp:Content>

<asp:Content ID=headerContent ContentPlaceHolderID=HeaderContent runat=server>

</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    
<h2><%= Html.Encode(ViewData["Message"]) %></h2>
    
<p>
        
To learn more about ASP.NET MVC visit <a href="http://asp.net/mvc" title="ASP.NET MVC Website">http://asp.net/mvc</a>.
    
</p>
</asp:Content>

 

  在center标记处,我们嵌入了Silverlight对象,首先我们要引用Silverlight.js文件。

<script type="text/javascript" src="Silverlight.js"></script>

  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);
}

 

  然后我又在Index.aspx中插入下面的代码:

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    
<h2>
        
<%= Html.Encode(ViewData["Message"]) %></h2>
    
<p>
        
To learn more about ASP.NET MVC visit <a href="http://asp.net/mvc" title="ASP.NET MVC Website">
            http:
//asp.net/mvc</a>.
    
</p>
    
<div id="silverlightControlHost">
        
<object data="data:application/x-silverlight-2," type="application/x-silverlight-2"
            width
="100%" height="100%">
            
<param name="source" value="ClientBin/CloudySilverlight.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>
</asp:Content>
0
相关文章