1

我是 Script# 的新手,我编写了以下示例代码来学习使用 Script# 的事件处理:

public class Class1
{    
    private bool ispressed;
    private int startX;
    private int startY;
    private int endX;
    private int endY;

    private CanvasElement canvas;
    private CanvasContext2D context;
    private Element createNumberLine;
    public Class1()
    {
        ispressed = false;

        canvas = (CanvasElement)Document.CreateElement("canvas");
        canvas.ID = "canvas";
        canvas.SetAttribute("width", "500");
        canvas.SetAttribute("height", "500");
        canvas.Style.Border = "2px Solid #000000";
        Document.Body.AppendChild(canvas);
        context = (CanvasContext2D)canvas.GetContext("2d");

        canvas.AddEventListener("click", CreateNumberLine, false);

        createNumberLine = (Element)Document.CreateElement("input");
        createNumberLine.SetAttribute("type", "button");
        createNumberLine.SetAttribute("width", 100);
        createNumberLine.SetAttribute("height", 50);
        createNumberLine.SetAttribute("value", "Create Number Line");
        createNumberLine.AddEventListener("click", CreateNumberLine, false);
        Document.Body.AppendChild(createNumberLine);



    }
    ...
    ...
    private void CreateNumberLine(ElementEvent e)
    {
        Script.Alert("asd");
        //context.Restore();
        startX = 100;
        endX = 700;
        startY = 200;
        endY = 200;
        context.MoveTo(startX, startY);
        context.LineTo(endX, endY);
        context.StrokeStyle = "#00ff00";
        context.Stroke();       
    }
}

这是我的 HTML:

<head>
<title></title>
<script src="Scripts/debug/mscorlib.debug.js" type="text/javascript"></script>
<script src="Scripts/debug/ScriptLibrary1.debug.js" type="text/javascript"></script>
</head>
<body>
<script type="text/javascript">
var script;
window.onload = function () {
    script = new ScriptLibrary1.Class1();
}
</script>
</body>

但是当我在浏览器中运行此代码并单击按钮时,我得到一个 Uncaught TypeError: Object # has no method 'apply' 异常。我没有使用任何第三方 javascript 库。我无法弄清楚这段代码有什么问题。一些帮助将不胜感激。谢谢。

4

1 回答 1

0

我得到了这个问题的答案。

这一行:

createNumberLine.AddEventListener("click", CreateNumberLine, false);

应替换为:

if (Script.isNotNullofUnDefined)
{
     createNumberLine.AddEventListener("click", CreateNumberLine, false);
}

或者

createNumberLine.AddEventListener("click", delegate(ElementEvent e)
{
    CreateNumberLine;
}, false);
于 2013-07-24T09:38:05.913 回答