2016-10-18 27 views

回答

5

您需要做的是將<ListPicker>items屬性綁定到頁面的bindingContext上的數組。這裏有一個簡單的例子:

<!-- main-page.xml --> 
<Page xmlns="http://schemas.nativescript.org/tns.xsd" loaded="pageLoaded"> 
    <StackLayout> 
    <ListPicker 
     items="{{ pokemon }}" 
     id="pokemonPicker" 
    ></ListPicker> 
    </StackLayout> 
</Page> 

// main-page.js 
var Observable = require("data/observable").Observable; 

var pageData = new Observable({ 
    pokemon: ["Bulbasaur", "Charmander", "Squirtle"] 
}); 

exports.pageLoaded = function(args) { 
    var page = args.object; 
    page.bindingContext = pageData; 

    page.getViewById("pokemonPicker").addEventListener(
    Observable.propertyChangeEvent, function(e) { 
     if (e.propertyName == "selectedIndex") { 
     console.log("You selected: " + pageData.pokemon[e.value]); 
     } 
    } 
); 
};