2016-04-09 33 views
0

我正在尝试编写一个本地脚本应用程序,该应用程序在我的xml中检索bottle视图/元素的渲染高度。我尝试了下面的代码,但不幸的是,我得到一个错误(如下所示)。任何指导将不胜感激。在NativeScript中访问元素高度

JS:

var viewModule = require("ui/core/view"); 
var page; 

exports.fabTap = function (args) { 
     page = args.object; 
     var bottle = page.getViewById("bottle"); 
     console.log("Height: " + bottle.height); 
    } 

XML:

<Page xmlns="http://schemas.nativescript.org/tns.xsd" xmlns:FAB="nativescript-floatingactionbutton" actionBarHidden="true" loaded="pageLoaded"> 
    <GridLayout columns="*, *, *, *" rows="15*, 5*, 20*, 5*, 5*, 5*, 5*, 20*, 20*" width="100%" height="100%" style.backgroundColor="white" > 
    <Image src="res://logo" row="0" col="1" colSpan="2" stretch ="aspectFit" class="logo"/> 
    <Image id="bottle" src="res://bottle_outline" row="2" col="0" rowSpan="6" colSpan="2" stretch="aspectFit"/> 
    </GridLayout> 
</Page> 

无法读取属性 '高度' 的定义。

回答

1

这是因为在fabTap处理程序中,args.object不是页面而是FAB窗口小部件本身。所以,当你调用getViewById它搜索FAB小部件的视图层次,并没有瓶有:)你应该改变你的代码如下:

var viewModule = require("ui/core/view"); 
var page; 
exports.pageLoaded = function (args) { 
    page = args.object; 
} 
exports.fabTap = function (args) { 
    var bottle = page.getViewById("bottle"); 
    console.log("Height: " + bottle.height); 
} 
+0

感谢在清除了,这似乎已经解决了这个不确定问题。但是我现在将'Height:NaN'打印到日志中,为什么会这样呢? –

+1

@GeorgeEdwards似乎'高度'只会返回,如果你设置属性,而不是返回实际的。尝试打印'bottle.getMeasuredHeight()',看看它是否有所作为。 –