用javascript寫Android和iOS naitve應用,實在炫酷。

關注NativeScript有一段時間了,說好了的三月發第一個Beta版,終於發佈了。

// declare the extended NativeScriptActivity functionality
var 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 instance
var 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 context
app.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>

它還支持“數據綁定”,這你敢信?!

上樣例:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章