未捕获的错误:XXX实例的imageWidth属性尚未准备好



我有一个扩展qx.ui.basic.Image的自定义类,代码如下:

/*
#asset(path/to/*)
*/
qx.Class.define("myApp.myImage",
{
  extend : qx.ui.basic.Image,
  construct: function() {
    this.base(arguments);
    var imgPath = "path/to/test.png";
    this.setSource(imgPath);
    this.imageWidth = qx.util.ResourceManager.getInstance().getImageWidth(imgPath);
  },
  properties : {
    imageWidth : { check : 'Integer' }
  }
});

当我尝试使用:

var img = new myApp.myImage();
console.log(img.getImageWidth());

,

未捕获错误:myApp实例的imageWidth属性。模板是还没准备好

我只想获得图像的图像宽度。我还尝试使用:

var img = new myApp.myImage();
console.log(img.getBounds().width);

出现的错误是:

无法获取未定义的宽度

我错过了什么?

让我们看一下属性描述。它没有init值,也不能为空。这意味着,当你创建它时,它处于某种未知状态,没有任何价值。像在示例中那样查询该属性会导致错误,因为属性系统不知道要返回什么值,因为您没有指定一个。所以这都要到构造函数的最后一行在那里你设置了一个名为imageWidth的成员变量为图像的正确宽度。但这不是属性,它是一个普通成员。因此,您可以访问示例中的成员变量,如img.imageWidth,或者在构造函数中使用属性的setter: this.setImageWidth(...)

最新更新