我在实现共享SideDrawer时遇到问题。我在关注这篇文章https://www.nativescript.org/blog/a-deep-dive-into-telerik-ui-for-nativescripts-sidedrawer

主页XML:

<Page
  xmlns="http://schemas.nativescript.org/tns.xsd"
  xmlns:sd="nativescript-telerik-ui/sidedrawer" loaded="pageLoaded">
  <Page.actionBar>
    <ActionBar class="action-bar" title="Manu">
      <android>
        <NavigationButton icon="res://ic_menu" tap="toggleDrawer" />
      </android>
      <ios>
        <ActionItem icon="res://ic_menu" ios.position="left" tap="toggleDrawer" />
      </ios>
    </ActionBar>
  </Page.actionBar>
  <sd:RadSideDrawer id="sideDrawer">
    <sd:RadSideDrawer.drawerContent
      xmlns:Drawer="Views/SideDrawerView">
      <Drawer:SideDrawerView />
    </sd:RadSideDrawer.drawerContent>
  </sd:RadSideDrawer>
  <StackLayout>
    <Label text="{{ sync }}" />
  </StackLayout>
</Page>

主页背后的代码:
import { EventData } from 'data/observable';
import { Page } from 'ui/page';
import { MainViewModel } from '../../ViewModels/MainViewModel';

let frameModule = require("ui/frame");
let view = require("ui/core/view");
let page: Page;

export function pageLoaded(args: EventData) {
    page = <Page>args.object;
    page.bindingContext = new MainViewModel();
}

export function toggleDrawer () {
    let drawer: any = view.getViewById(page, "sideDrawer");
    drawer.toggleDrawerState();
};

SideDrawer XML:
<StackLayout class="sidedrawer-left" loaded="drawerViewLoaded">
    <StackLayout class="sidedrawer-header">
        <Label text="Hello RadSideDrawer!" class="sidedrawer-header-brand" />
    </StackLayout>
    <StackLayout class="sidedrawer-content">
        <Label text="QrScanner" class="sidedrawer-list-item-text" tap="qrScan" />
        <Label text="New Item" class="sidedrawer-list-item-text" tap="nItem" />
        <Label text="Edit Item" class="sidedrawer-list-item-text" tap="eItem" />
        <Label text="Users" class="sidedrawer-list-item-text" tap="openUsers" />
        <Label text="Map" class="sidedrawer-list-item-text" tap="map" />
    </StackLayout>
</StackLayout>

因此,当我实际点击并调用toggleDrawer函数时
export function toggleDrawer () {
    let drawer: any = view.getViewById(page, "sideDrawer");
    drawer.toggleDrawerState();
};

它说抽屉是不确定的。有什么事吗

最佳答案

尝试这种方式:)当您有Tap事件时,您可以在toggleDrawer中获取Tap事件数据,并从args中获取被窃听对象的page,从中可以访问sideDrawer控制或使用frameModuletopmost()函数

export function toggleDrawer (args: EventData) {
    //this way
    let page = <Page>(<any>args.object).page;
    //or this way
    let page = frameModule.topmost();
    //at last get drawer content at page
    let drawer: any = page.getViewById("sideDrawer");
    drawer.toggleDrawerState();
};

10-08 05:35