Silverlight不会调用OnLoad参数

时间:2016-07-27 01:17:39

标签: javascript c# asp.net silverlight

我已经设置了我的Silverlight ASPX页面:

<%@ Page Language="C#" MasterPageFile="~/ASPResources/MasterPage.master" AutoEventWireup="true"
CodeBehind="SilverlightPage.aspx.cs" Inherits="Adapt.Presentation.AssetInventory.SilverlightPage"
Title="Xivic" %>

<%@ MasterType VirtualPath="~/ASPResources/MasterPage.master" %>
<%@ Register Assembly="System.Web.Silverlight" Namespace="System.Web.UI.SilverlightControls"
TagPrefix="asp" %>
<asp:Content ID="Content2" ContentPlaceHolderID="mainContent" runat="server">

<script type="text/javascript" id="silverlightport">
    var silverlightControl = null;
    function pluginLoaded(sender, args)
    {
        silverlightControl = sender.getHost();
    }

    function externalCommand(data)
    {
        if (silverlightControl != null)
        {
            silverlightControl.Content.JSR.ExternalCommand(data);
        }
        else
        {
            alert("Couldn't get silverlight host object in SilverlightPage.aspx");
        }
    }
</script>

<asp:Silverlight ID="SilverlightLaunchPage" EnableGPUAcceleration="true" runat="server"
    Source="~/ClientBin/Xivic.xap" Width="100%" Height="100%" InitParameters="onLoad=pluginLoaded" />

Silverlight启动并且不能调用 pluginLoaded 函数,因为当我调用 externalCommand 函数时, silverlightControl 变量未设置。所以要么我的Silverlight页面没有加载,我很确定它有,或者我的onLoad事件不能正确挂钩,这更可能因为我几乎没有ASP知识。

有人可以请我填写我在这里做错的事吗?

1 个答案:

答案 0 :(得分:0)

以下是Silverlight 5控件的示例。我根据你的例子中的内容对其进行了调整

<head runat="server">
    <script type="text/javascript" src="..\Scripts\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>
    <script type="text/javascript" id="silverlightport">
        var silverlightControl = null;
        function pluginLoaded(sender, args)
        {
            silverlightControl = sender.getHost();
        }

        function externalCommand(data)
        {
            if (silverlightControl != null)
            {
                silverlightControl.Content.JSR.ExternalCommand(data);
            }
            else
            {
                alert("Couldn't get silverlight host object in SilverlightPage.aspx");
            }
        }
    </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/Xivic.xap" />
                <param name="onError" value="onSilverlightError" />
                <param name="onLoad" value="pluginLoaded" />
                <param name="background" value="#F2F2F2" />
                <param name="minRuntimeVersion" value="5.0.61118.0" />
                <param name="autoUpgrade" value="true" />
                <a href="http://go.microsoft.com/fwlink/?LinkID=149156&v=5.0.61118.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: 0; width: 0; border: 0"></iframe>
        </div>
    </form>
</body>

路径可能需要更改以匹配您的文件结构