NativeScript:绑定 SearchBar 提交事件与视图模型 function/method?

NativeScript: Binding SearchBar submit event with view-model function/method?

您好,我无法使用我在 vew-model.ts.

中声明的方法绑定 NativeScript Searchbar 提交事件
`<SearchBar id="searchBar" hint="Search" text=""  clear="onClear" submit="{{ onSubmit }}"/>`

And the function in viewmodel:

`public onSubmit(args) {
        console.log('pressed');
        let searchBar = <SearchBar>args.object;
        var searchPhrase = searchBar.text;
        this.url = this.url+this.searchPhrase;
        this.getItems(this.url);
        console.log(this.url);
    }`

但这似乎不起作用。如果我在代码隐藏文件 (main-page.ts) 中定义相同的方法,则无论我是否将其写为:

都会调用它
`<SearchBar id="searchBar" hint="Search" text=""  clear="onClear" submit=" onSubmit"/>`

`<SearchBar id="searchBar" hint="Search" text=""  clear="onClear" submit="{{ onSubmit }}"/>`

我是 NativeScript 的新手,我不知道我是否遗漏了什么。我确实检查了 答案,但没有帮助,因为那是针对 TextFields 的。任何帮助将不胜感激。谢谢!

onSubmit 应该是 ViewModel 的 属性 才能进行事件绑定。更新方法如下。

onSubmit = (args) => {
    console.log('pressed');
    let searchBar = <SearchBar>args.object;
    var searchPhrase = searchBar.text;
    this.url = this.url+this.searchPhrase;
    this.getItems(this.url);
    console.log(this.url);
};