包含Silverlight应用程序的Aspx页面的母版页

时间:2010-07-14 03:56:23

标签: asp.net silverlight

创建新的Silverlight应用程序时,VS2010会生成带有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>SilverlightApp2</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/SilverlightApp2.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>
</html>

我不确定如何修改页面以便它使用我的母版页

<%@ Page Language="C#"  MasterPageFile="~/Site.master" AutoEventWireup="true" %>

由于

4 个答案:

答案 0 :(得分:1)

回答为时已晚,但可能对寻求答案的其他人有所帮助。

所有JavaScript代码都转到标题占位符,DaveB先前已说明。

<asp:Content ID="Content1" ContentPlaceHolderID="MySiteMasterHeaderPlaceholder" Runat="Server">
    // JavaScript code goes here
</asp:Content>

您不在Content标记内使用第二个表单标记,因为它已在主页面上声明。

<asp:Content ID="Content2" ContentPlaceHolderID="MySiteMasterContentPlaceholder" Runat="Server">   
    <div id="silverlightControlHost">
        <object data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="730px" height="250px">
            <param name="source" value="MainAssetsSearch.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://go.microsoft.com/fwlink/?LinkID=149156&v=4.0.50826.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" />
    </div>
</asp:Content>

并且避免使用width="100%"height="250%",因为Firefox不会呈现Silverlight内容。

答案 1 :(得分:0)

它非常简单...只需将xap文件视为您想要的另一个Flash或任何其他第三方控件..

只需创建一个WebContentForm,从任何主页继承它,然后按照下面给出的关于如何在aspx页面中集成SL的文章中的链接。

以下是有关如何以各种方式集成XAP的详细信息: http://msdn.microsoft.com/en-us/library/cc838145%28VS.95%29.aspx

问候。

答案 2 :(得分:0)

这是你的建议吗?

<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApp2.WebForm1" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<form id="form1" runat="server" style="height:100%;">
  <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
    <div  style="height:100%;">
      <asp:Silverlight ID="Xaml1" runat="server" 
        Source="ClientBin/SilverlightApp2.xap" MinimumVersion="2.0" Width="100%" 
        Height="100%" />
   </div>
</form>
</asp:Content>

这给了我以下错误

The type or namespace name 'Silverlight' does not exist in the namespace 'System.Web.UI.WebControls' (are you missing an assembly reference?)   

然而,VS2010生成的上述页面在同一个项目中运行良好

答案 3 :(得分:0)

试一试。应该很接近。我们的想法是将您的样式和Javascript部分放在页面头部的内容控件中,并将Silverlight代码放在页面正文的内容控件中。

<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="BmwConfig.WebApp2.WebForm1" %> 
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server"> 

<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> 

</asp:Content> 
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> 
<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/SilverlightApp2.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> 
</asp:Content>