扩展Repeater(或其他具有嵌套XML标记属性的小部件)

时间:2018-12-13 19:48:07

标签: nativescript

我尝试扩展Repeater。只需这样做:

// views/widgets/MyRepeater/MyRepeater.ts
import {Repeater} from "tns-core-modules/ui/repeater";

export class MyRepeater extends Repeater {}

现在我尝试使用它:

<Page xmlns="http://schemas.nativescript.org/tns.xsd"
      xmlns:Test="views/widgets/MyRepeater/MyRepeater">
    <Test:MyRepeater items="{{ items }}">
        <Test:MyRepeater.itemsLayout>
            <StackLayout/>
        </Test:MyRepeater.itemsLayout>
        <Test:MyRepeater.itemTemplate>
            <Label text="{{ someBindingValue }}"></Label>
        </Test:MyRepeater.itemTemplate>
    </Test:MyRepeater>
</Page>

在第一次更改<ObservableArray>items之前不会产生任何错误。 我收到这样的错误:

JS: TypeError: Cannot set property 'bindingContext' of undefined
JS:     at MyRepeater.Repeater.refresh (file:///data/data/com.test/files/app/tns_modules/tns-core-modules/ui/repeater/repeater.js:48:38)
JS:     at MyRepeater.Repeater._requestRefresh (file:///data/data/com.test/files/app/tns_modules/tns-core-modules/ui/repeater/repeater.js:34:18)
JS:     at MyRepeater.Repeater._onItemsChanged (file:///data/data/com.test/files/app/tns_modules/tns-core-modules/ui/repeater/repeater.js:54:14)
JS:     at Object.handler [as callback] (file:///data/data/com.test/files/app/tns_modules/tns-core-modules/ui/core/weak-event-listener/weak-event-listener.js:30:34)
JS:     at Observable.notify (file:///data/data/com.test/files/app/tns_modules/tns-core-modules/data/observable/observable.js:110:23)
JS:     at ObservableArray.splice (file:///data/data/com.test/files/app/tns_modules/tns-core-modules/data/observable-array/observable-array.js:129:14)

就像在代码tns-core-modules/ui/repeater/repeater.js:48:38中一样,它尝试解析itemTemplate,但不能这样做。

我做错了什么?

2 个答案:

答案 0 :(得分:1)

您需要像这样从tns-core-modules/ui/repeater重新导出所有文件: export * from 'tns-core-modules/ui/repeater'

答案 1 :(得分:0)

我认为您的自定义中继器文件应像original implementation中那样导出sharedPrefs.getInt("id")模块,以帮助XML解析器识别模板。

knownTemplates