Question

I am new to Script#, and I wrote following sample code to learn event handling using 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();       
    }
}

And this is my 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>

But when I run this code in the browser and click on the button, I get an Uncaught TypeError: Object # has no method 'apply' exception. I am not using any third party javascript libraries. I am unable to figure out what's wrong with this code. Some help will be appreciated. Thanks.

Was it helpful?

Solution

I got the answer for this question.

this line :

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

should be replaced by :

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

or

createNumberLine.AddEventListener("click", delegate(ElementEvent e)
{
    CreateNumberLine;
}, false);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top