我是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#没有方法'apply'异常。我没有使用任何第三方JavaScript库。我无法弄清楚这段代码有什么问题。一些帮助将不胜感激。谢谢。
答案 0 :(得分:0)
我得到了这个问题的答案。
这一行:
createNumberLine.AddEventListener("click", CreateNumberLine, false);
应替换为:
if (Script.isNotNullofUnDefined)
{
createNumberLine.AddEventListener("click", CreateNumberLine, false);
}
或
createNumberLine.AddEventListener("click", delegate(ElementEvent e)
{
CreateNumberLine;
}, false);