Nativescript 获取生成视图的高度

Nativescript 获取生成视图的高度,nativescript,angular2-nativescript,nativescript-telerik-ui,nativescript-angular,nativescript-cli,Nativescript,Angular2 Nativescript,Nativescript Telerik Ui,Nativescript Angular,Nativescript Cli,我正在尝试获取堆栈布局的高度。这个布局充满了来自我后端的数据,所以它的高度并不总是相同的。我尝试了view.getViewById(父级,选择器).effectiveHeight,但它一直输出0 <ListView [items]="a" class="list-group"> <ng-template let-country="item" let-i="index" let-odd="odd" let-even="even"> <Wrap

我正在尝试获取
堆栈布局的高度。这个布局充满了来自我后端的数据,所以它的高度并不总是相同的。我尝试了
view.getViewById(父级,选择器).effectiveHeight
,但它一直输出
0

<ListView [items]="a"  class="list-group">
    <ng-template let-country="item" let-i="index" let-odd="odd" let-even="even">
        <WrapLayout  width="100%"  orientation="horizontal" backgroundColor="palegreen">
            <StackLayout  #abc  (loaded)="getSize($event)" width="20%" [height]="testWidth" backgroundColor="lightgray">
                <Label [text]="'W'+testWidth" backgroundColor="red"></Label>
                <Label text="LA" backgroundColor="green"></Label>
            </StackLayout>
        </WrapLayout>
    </ng-template>
</ListView>
我的XML文件看起来有点像这样:

<Page>
    <ActionBar title="..."></ActionBar>
    <GridLayout rows="*, auto" columns="*">
        <ScrollView row="0" col="0">
            <GridLayout rows="*" columns="*">
                <StackLayout id="TARGET" row="0" col="1">
                    <StackLayout *ngFor="let x of y">
                        <FlexboxLayout justifyContent="space-between">
                            <FlexboxLayout>
                                <StackLayout>
                                    ...
                                </StackLayout>
                            </FlexboxLayout>

                            <FlexboxLayout justifyContent="space-between">
                                ...
                            </FlexboxLayout>

                        </FlexboxLayout>
                    </StackLayout>   
                </StackLayout>
                <StackLayout col="0" row="0">
                    ...
                </StackLayout>
            </GridLayout>
        </ScrollView>

        <StackLayout row="1" col="0">
            ...
        </StackLayout>
    </GridLayout>
</Page>
<ListView [items]="a"  class="list-group">
    <ng-template let-country="item" let-i="index" let-odd="odd" let-even="even">
        <WrapLayout  width="100%"  orientation="horizontal" backgroundColor="palegreen">
            <StackLayout  #abc  (loaded)="getSize($event)" width="20%" [height]="testWidth" backgroundColor="lightgray">
                <Label [text]="'W'+testWidth" backgroundColor="red"></Label>
                <Label text="LA" backgroundColor="green"></Label>
            </StackLayout>
        </WrapLayout>
    </ng-template>
</ListView>

...
...
...
...

提前谢谢你

加载视图后,使用
.getMeasuredHeight()

<ListView [items]="a"  class="list-group">
    <ng-template let-country="item" let-i="index" let-odd="odd" let-even="even">
        <WrapLayout  width="100%"  orientation="horizontal" backgroundColor="palegreen">
            <StackLayout  #abc  (loaded)="getSize($event)" width="20%" [height]="testWidth" backgroundColor="lightgray">
                <Label [text]="'W'+testWidth" backgroundColor="red"></Label>
                <Label text="LA" backgroundColor="green"></Label>
            </StackLayout>
        </WrapLayout>
    </ng-template>
</ListView>

来源:

如果您使用的是
view.getViewById(父级,选择器)。effectiveHeight
只有在呈现和加载视图时才有效。即使您试图获取
load()
事件的值,由于nativescript错误,它也将为0。
<ListView [items]="a"  class="list-group">
    <ng-template let-country="item" let-i="index" let-odd="odd" let-even="even">
        <WrapLayout  width="100%"  orientation="horizontal" backgroundColor="palegreen">
            <StackLayout  #abc  (loaded)="getSize($event)" width="20%" [height]="testWidth" backgroundColor="lightgray">
                <Label [text]="'W'+testWidth" backgroundColor="red"></Label>
                <Label text="LA" backgroundColor="green"></Label>
            </StackLayout>
        </WrapLayout>
    </ng-template>
</ListView>
为了解决这个问题,我建议将超时设置为200毫秒。 比如说

<ListView [items]="a"  class="list-group">
    <ng-template let-country="item" let-i="index" let-odd="odd" let-even="even">
        <WrapLayout  width="100%"  orientation="horizontal" backgroundColor="palegreen">
            <StackLayout  #abc  (loaded)="getSize($event)" width="20%" [height]="testWidth" backgroundColor="lightgray">
                <Label [text]="'W'+testWidth" backgroundColor="red"></Label>
                <Label text="LA" backgroundColor="green"></Label>
            </StackLayout>
        </WrapLayout>
    </ng-template>
</ListView>
home.component.html

<ListView [items]="a"  class="list-group">
    <ng-template let-country="item" let-i="index" let-odd="odd" let-even="even">
        <WrapLayout  width="100%"  orientation="horizontal" backgroundColor="palegreen">
            <StackLayout  #abc  (loaded)="getSize($event)" width="20%" [height]="testWidth" backgroundColor="lightgray">
                <Label [text]="'W'+testWidth" backgroundColor="red"></Label>
                <Label text="LA" backgroundColor="green"></Label>
            </StackLayout>
        </WrapLayout>
    </ng-template>
</ListView>

代码示例还显示了如何动态获取StackLayout的高度和宽度,并将其绑定到视图。

在按id选择的视图上尝试此操作会出现以下错误:
[ts]属性“getMeasuredHeight”在类型“ViewBase”上不存在。
如果您可以创建一个游乐场示例来演示您的问题,我可能会解决此问题。
<ListView [items]="a"  class="list-group">
    <ng-template let-country="item" let-i="index" let-odd="odd" let-even="even">
        <WrapLayout  width="100%"  orientation="horizontal" backgroundColor="palegreen">
            <StackLayout  #abc  (loaded)="getSize($event)" width="20%" [height]="testWidth" backgroundColor="lightgray">
                <Label [text]="'W'+testWidth" backgroundColor="red"></Label>
                <Label text="LA" backgroundColor="green"></Label>
            </StackLayout>
        </WrapLayout>
    </ng-template>
</ListView>