在Nativescript中创建组件的正确方法是什么?

时间:2016-11-22 14:55:37

标签: nativescript

Nativescript中创建自定义模板的正确方法是什么?

例如,我有XML结构,如:

     <FlexboxLayout flexDirection="column" alignItems = "stretch">
        <Border  borderWidth="1" borderColor="#000000">
        <FlexboxLayout height="60">
            <Button cssClass="sdk-button" col="0" text="initSDK" tap="{{initSdk}}" flexGrow="1"/>

            <StackLayout orientation="horizontal" class="data-wrapper" flexGrow="3" paddingLeft="20">
                <Label text="{{ initSdkResponse }}" class="data_wrapper-label" textWrap="true"/>
            </StackLayout>
        </FlexboxLayout>
        </Border>
    </FlexboxLayout>

我想创建具有上述结构的组件,名为MyItem并使用如下:

<Page xmlns="http://schemas.nativescript.org/tns.xsd" navigatingTo="onNavigatingTo">
    <StackLayout orientation="vertical">
      <MyItem buttonName="click1"
              buttonAction="onClick1()"
              labelText="xxxxxxx1"
      ></MyItem>

      <MyItem buttonName="click2"
              buttonAction="onClick2()"
              labelText="xxxxxxx2"
      ></MyItem>
    </StackLayout>
</Page>