Nativescript - 单击按钮打开 SideDrawer

Nativescript -Open SideDrawer on button click

我正在使用 telerik ui 作为本机脚本。我需要顶部的切换按钮来打开侧面菜单。但我无法根据文档调用 Showdrawer() 。 我需要的是按钮点击侧菜单应该打开。我尝试调用 RadSideDrawer.prototype.showDrawer(),但失败了。 Nativescript 是否还有其他可用的侧边菜单?

主要-page.xml

<Page xmlns="http://www.nativescript.org/tns.xsd" xmlns:drawer="nativescript-telerik-ui/sidedrawer" loaded="pageLoaded">
    <Page.actionBar>
        <ActionBar>
            <android>
                <NavigationButton text="Go Back" android.systemIcon="ic_menu_moreoverflow" tap="showSideDrawer" />
            </android>
        </ActionBar>
    </Page.actionBar>
    <drawer:RadSideDrawer>
        <drawer:RadSideDrawer.mainContent>
            <StackLayout>
                <Label text="{{ mainContentText }}" textWrap="true" />
            </StackLayout>
        </drawer:RadSideDrawer.mainContent>
        <drawer:RadSideDrawer.drawerContent>
            <StackLayout cssClass="drawerContent" style="background-color:white;">
                <StackLayout cssClass="headerContent">
                    <Label text="Header" />
                </StackLayout>
                <StackLayout cssClass="drawerMenuContent">
                    <Label text="Item 1" style="color:black;" />
                    <Label text="Item 2" style="color:black;" />
                    <Label text="Item 3" style="color:black;" />
                    <Label text="Item 4" style="color:black;" />
                </StackLayout>
            </StackLayout>
        </drawer:RadSideDrawer.drawerContent>
    </drawer:RadSideDrawer>
</Page>

入门-model.js

var observableModule = require("data/observable");
var GettingStartedViewModel = (function (_super) {
    __extends(GettingStartedViewModel, _super);
    function GettingStartedViewModel() {
        _super.call(this);
        this.set("mainContentText", "SideDrawer for NativeScript can be easily setup in the XML definition of your page by defining main- and drawer-content. The component"
            + " has a default transition and position and also exposes notifications related to changes in its state.");

    }
    return GettingStartedViewModel;
})(observableModule.Observable);
exports.GettingStartedViewModel = GettingStartedViewModel;
function showSideDrawer(args) {
    console.log("Show SideDrawer tapped.");
    // Show sidedrawer ...
   _super.prototype.showDrawer.call(this);

}
exports.showSideDrawer = showSideDrawer;

主要page.js

var viewModelModule = require("./getting-started-model");
function pageLoaded(args) {
    console.log("Page loaded");
    var page = args.object;
    page.bindingContext = new viewModelModule.GettingStartedViewModel();
}
exports.pageLoaded = pageLoaded;

您是否从您没有使用的代码中调用 showSideDrawer 函数 post?您确定链接了点击按钮吗?

<Button tap="showSideDrawer" text="ToggleDrawer"/>

也许你可以尝试给 sideDrawer 一个 Id 并使用这个代码。

var drawer = frameModule.topmost().getViewById("sideDrawer");
drawer.showDrawer();

您可以查看显示 RadSideDrawer 主要功能的 this SDK examples。正如@R Pelzer 所提到的,您需要做的就是获取 RadSideDrawer 的实例,例如使用其 id:

import drawerModule = require("nativescript-telerik-ui-pro/sidedrawer");
import frameModule = require("ui/frame");

var sideDrawer: drawerModule.RadSideDrawer = <drawerModule.RadSideDrawer>(frameModule.topmost().getViewById("sideDrawer"));

并调用它的showDrawer()方法:

sideDrawer.showDrawer();

你得到 undefined 因为没有为抽屉分配 id 所以要解决你的问题,请为 sideDrawer 分配一个 id <drawer:RadSideDrawer id="sideDrawer"> 然后你可以调用

var frame = require('ui/frame');
var drawer = frame.topmost().getViewById("sideDrawer");

function showSideDrawer(){
drawer.showDrawer();  // i prefer using .toggleDrawerState();
};

在我的例子中,我错过了插入,因此在调用 toggleDrawer 时缺少组件,因此出现错误 "TypeError: Cannot read property 'toggleDrawerState' of undefined"。

尝试插入 xml 文件的所有正文部分可能会解决问题。

编码愉快:))