標籤:
關注NativeScript有一段時間了,說好了的三月發第一個Beta版,終於發布了。
// declare the extended NativeScriptActivity functionalityvar extendsObject = { onCreate: function(savedState){ // call the base NativeScriptActivity.onCreate method // the "this" variable points to a NativeScriptActivity instance this.super.onCreate(savedState); // create a button and set it as the main content var button = new android.widget.Button(this); button.setText("Hello World"); this.setContentView(button); }}// pass the extends object to create a new NativeScriptActivity instancevar mainActivity = com.tns.NativeScriptActivity.extends(extendsObject);var applicationInitObject = { getActivity: function(intent) { // this method is called whenever a new instance of NativeScriptActivity is about to be created return mainActivity; }, onCreate: function() { // This is the first method called. Called from the android.app.Application.onCreate method. } }// The NativeScriptRuntime exposes the app object within the global contextapp.init(applicationInitObject);
如上面代碼,用javascript創那建一個Android的Activity,是不是很炫酷!
當然也可以寫iOS native應用,這還不是最炫酷的,更炫酷的是,它還提供了一套“公用的”api,和一套可以用xml描述的ui控制項陳列庫,可以這樣定義介面,並在不同的平台下產生相應用本地控制項。
<Page xmlns="http://www.nativescript.org/tns.xsd" loaded="pageLoaded"> <StackLayout> <Label text="Tap the button" cssClass="title"/> <Button text="TAP" tap="{{ tapAction }}" /> <Label text="{{ message }}" cssClass="message" textWrap="true"/> <Button text="test button"/> </StackLayout></Page>
它還支援“資料繫結”,這你敢信?!
上範例:
用javascript寫Android和iOS naitve應用,實在炫酷。