Nativescript自定义组件子

时间:2017-03-06 10:57:31

标签: nativescript

您好我正在尝试使用这样的自定义组件。但我无法使用 getViewById 访问元素,返回undefined。并且无法在main-page.js上捕捉到点击事件。

Sidemenu.xml

<dpg:DrawerPage
    xmlns:dpg="nativescript-telerik-ui/sidedrawer/drawerpage"
    xmlns:drawer="nativescript-telerik-ui/sidedrawer"
    xmlns:sidemenu="components/sidemenu"
    xmlns="http://www.nativescript.org/tns.xsd">

    <dpg:DrawerPage.sideDrawer >
        <drawer:RadSideDrawer id="sideDrawer">
            <drawer:RadSideDrawer.drawerContent>
                <StackLayout cssClass="drawerContent">

                    <StackLayout cssClass="headerContent" style="width: 80%;">
                        <Label text="Navigation Menu"/>
                    </StackLayout>
                    <ScrollView>
                        <StackLayout cssClass="drawerMenuContent">
                            <Label text="Primary" cssClass="drawerSectionHeader" />
                            <Label text="Social" cssClass="drawerSectionItem" />
                            <Label text="Promotions" cssClass="drawerSectionItem" />
                            <Label text="Labels" cssClass="drawerSectionHeader" />
                            <Label text="Important" cssClass="drawerSectionItem" />
                            <Label text="Starred" cssClass="drawerSectionItem" />
                            <Label text="Sent Mail" cssClass="drawerSectionItem" />
                            <Label text="Drafts" cssClass="drawerSectionItem" />
                        </StackLayout>
                    </ScrollView>

                    <Button text="Close Drawer" tap="tap"/>

                </StackLayout>
            </drawer:RadSideDrawer.drawerContent>
        </drawer:RadSideDrawer>
    </dpg:DrawerPage.sideDrawer>


</dpg:DrawerPage>

主page.xml

<sidemenu:sidemenu xmlns:sidemenu="components/sidemenu"
    class="page" loaded="onLoaded" navigatedTo="onNavigatedTo">

    <StackLayout orientation="vertical">

            <Button text="Button 1" id="btn1">

    </StackLayout>

</sidemenu:sidemenu>

主page.js

exports.onLoaded = function(args){

    page = args.object;
        page.bindingContext = viewModel;

        console.log(page.getViewById("btn1")); //undefined

};

1 个答案:

答案 0 :(得分:0)

你可以这样试试吗?

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

exports.onLoaded = function (args) {
  var page = args.object;
  var button1 = view.getViewById(page, "btn1");
}