Events “相同的数据例程”;CalcPrecio“;在Lightswitch HTML客户端中,从按钮调用工作正常,但从画布事件调用时中断

Events “相同的数据例程”;CalcPrecio“;在Lightswitch HTML客户端中,从按钮调用工作正常,但从画布事件调用时中断,events,canvas,visual-studio-lightswitch,jscript,Events,Canvas,Visual Studio Lightswitch,Jscript,这是基于Lightswitch HTML客户端的jscript 从按钮调用相同的例程可以正常工作,但从画布事件调用时失败,显示未定义的数据错误 // Creates a Canvas and add an eventlistner Works fine myapp.AddEditLito.Montajes_render = function (element, contentItem) { var itemTemplate = $("<canvas id='myCanvas' width

这是基于Lightswitch HTML客户端的jscript 从按钮调用相同的例程可以正常工作,但从画布事件调用时失败,显示未定义的数据错误

// Creates a Canvas and add an eventlistner Works fine

myapp.AddEditLito.Montajes_render = function (element, contentItem) {
var itemTemplate = $("<canvas id='myCanvas' width='600' height='150'></canvas>");

    var canvas = myCanvas;
    myCanvas.addEventListener('mousedown', function (event) {
    var context = canvas.getContext('2d');
    DibTriangulo(screen, 50); // Draws a triangle, everything fine until here

    // here is the problem, 
    CalcPrecio(screen, 1); // breaks with "Undefined Data.. etc."
    }, false);
};

// Called from a Button, same routine works perfect!!

myapp.AddEditLito.Button1_execute = function (screen) { 
    CalcPrecio(screen, 1); // Works fine
};

// This routine Works perfect called from a button, but fails when called from a Canvas event!

function CalcPrecio(screen, col) {
    $.each(screen.Liquidas.data, function (i, p) {
        p.valor = p.cantidad * p.unitario;
    });
};
//创建画布并添加eventlistner效果良好
myapp.AddEditLito.Montajes_render=函数(元素,内容项){
var itemTemplate=$(“”);
var canvas=myCanvas;
myCanvas.addEventListener('mousedown',函数(事件){
var context=canvas.getContext('2d');
DibTriangula(屏幕,50);//画一个三角形,在这里之前一切都很好
//问题是,
CalcPrecio(屏幕,1);//以“未定义的数据..等”中断
},假);
};
//从一个按钮调用,同样的例程工作完美!!
myapp.AddEditLito.Button1\u execute=函数(屏幕){
CalcPrecio(屏幕,1);//工作正常
};
//这个例程从按钮调用时工作正常,但从画布事件调用时失败!
功能CalcPrecio(屏幕、列){
$.each(screen.Liquidas.data,function(i,p){
p、 valor=p.cantidad*p.unitario;
});
};

//我做错了什么?嘿

此问题是从canvas的mousedown事件处理程序调用DibTriangula和CalcPrecio例程的结果,第一个参数是screen

在mousedown事件处理程序和中定义的_render中,screen对象没有专门设置为引用LightSwitch屏幕实例,而只是引用该对象

要解决此问题,只需将代码更改为使用contentItem.screen,如以下修订的渲染方法所示:

myapp.AddEditLito.Montajes_render = function (element, contentItem) {
    var itemTemplate = $("<canvas id='myCanvas' width='600' height='150'></canvas>");

    var canvas = myCanvas;
    myCanvas.addEventListener('mousedown', function (event) {
        var context = canvas.getContext('2d');

        DibTriangulo(contentItem.screen, 50); // Changed to pass in contentItem.screen

        CalcPrecio(contentItem.screen, 1); // Changed to pass in contentItem.screen
    }, false);
};
myapp.AddEditLito.Montajes_render=函数(元素,内容项){
var itemTemplate=$(“”);
var canvas=myCanvas;
myCanvas.addEventListener('mousedown',函数(事件){
var context=canvas.getContext('2d');
DibTriangula(contentItem.screen,50);//更改为传入contentItem.screen
CalcPrecio(contentItem.screen,1);//更改为传入contentItem.screen
},假);
};
在按钮的_execute方法中使用just screen的原因是_execute方法接收LightSwitch screen对象作为其参数(然后在调用CalcRecio方法时使用该对象)