开发者

How can I put a Silverlight Application into a ASP MVC view page?

I'm working with both silverlight and asp mvc. The silverlight application performs a page request, and it works fine (into the test page generated by the VS), but I can't find a way to put it into my view.

MiniSIG

<div id="silverlightControlHost"> 

    <object data="data:application/x-silverlight-2," 开发者_运维百科type="application/x-silverlight-2" width="100%" height="100%">


      <param name="source" value="/ClientBin/MiniSIG.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>

</div> 

I tried almost everything (from putting the whole thing in the Site.Master page to loading the Silverlight.js from there). Can anywone give me some advice on how should I do this?

My view now looks like:

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <h2>MiniSIG</h2>

    <div id="silverlightControlHost"> 

        <object data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="100%" height="100%">


          <param name="source" value="/ClientBin/MiniSIG.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>

    </div> 

</asp:Content>

And my Site.Master:

<head runat="server">
    <title><asp:ContentPlaceHolder ID="TitleContent" runat="server" /></title>
    <link href="../../Content/Site.css" rel="stylesheet" type="text/css" />
     <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>

I can't see any problem here, the div names are the same.


One mistake I see is that you should have a forward slash before the ClientBin folder...

<param name="source" value="/ClientBin/MiniSIG.xap"/>

Also, I'd include this code to ensure Firefox compatibility:

<style type="text/css">     
#silverlightControlHost
{
  height: 100%;
}
</style>

<div id="silverlightControlHost"> 
  <!-- Silverlight object goes here -->
</div> 

(Otherwise the Silverlight app will be collapsed and invisible in Firefox.)


I solved the problem using half the solution Steve Wortham gave: instead of typing 100%, I typed 480px, which is the size of my Silverlight application, that now works on Firefox too.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜