我对Nativescript事件处理有点困惑。我正在使用明喻分离代码隐藏和viewmodel的typescript,我正在尝试将itemTap属性绑定到viewmodel方法(就像https://github.com/NativeScript/template-hello-world-ts/blob/master/main-view-model.ts中显示的示例一样)。
XML:
<Page xmlns="http://schemas.nativescript.org/tns.xsd" navigatingTo="navigatingTo" loaded="pageLoaded" class="page">
<StackLayout>
<ListView items="{{ items }}" itemTap="onItemTapped">
<ListView.itemTemplate>
<Label text="{{ Name }}"/>
</ListView.itemTemplate>
</ListView>
</StackLayout>
</Page>
代码隐藏:
import { EventData } from 'data/observable';
import { Page } from 'ui/page';
import { EditItemViewModel } from '../../ViewModels/EditItemViewModel';
export function navigatingTo(args: EventData) {
let page = <Page>args.object;
page.bindingContext = new EditItemViewModel();
}
视图模型:
import { Observable } from 'data/observable';
import { ObjectRelationalMapper } from '../Common/Data'
import { WebServiceDataManager } from '../Common/Data'
import { Item } from '../Models/Item';
export class EditItemViewModel extends Observable {
_wsDataManager: WebServiceDataManager;
_orm: ObjectRelationalMapper;
_items: Array<Item>;
get items(): Array<Item> {
return this._items;
}
set items(items: Array<Item>) {
this._items = items;
}
constructor() {
super();
this._wsDataManager = new WebServiceDataManager();
this._orm = new ObjectRelationalMapper();
this.listAllItems();
}
private listAllItems(): void {
this._orm.getAllObjects(Item, this, this.onItemsReceived)
}
public onItemsReceived(items: Array<Item>) {
this._items = items;
this.notify({ object: this, eventName: Observable.propertyChangeEvent, propertyName: 'items', value: this._items });
}
public onItemTapped(args): void {
let tappedView: any = args.view;
let tappedItem: Item = tappedView.bindingContext;
}
}
onItemTapped
方法不会被解雇。如果我将onItemTapped
放在代码隐藏中:
export function onItemTapped(args): void {
let tappedView: any = args.view;
let tappedItem: any = tappedView.bindingContext;
}
..它按预期工作。在viewModel中使用事件处理程序有什么问题,为什么&#34; HelloWorld&#34;例子如此误导?
答案 0 :(得分:4)
首先,你的 onItemTapped 来自你的视图模型。因此,您应该引用它,就像引用视图模型属性项和名称一样,这意味着使用双花括号:
<ListView items="{{ items }}" itemTap="{{ onItemTapped }}">
现在,如果 onItemTapped 函数放在您的代码隐藏文件中,那么您可以直接引用它,如代码所示:
<ListView items="{{ items }}" itemTap="onItemTapped">