Visual Studio開發Cordova應用示例

做者:Greyjavascript

原文地址:http://www.cnblogs.com/greyzeng/p/5455728.htmlcss

本文的GIF動畫均使用ScreenToGif進行錄製。html

Cordova是什麼?

http://cordova.apache.org/docs/en/latest/guide/overview/index.htmljava

實例說明

  • HelloWorld
  • 拍照

開發環境

HelloWorld

Visual Studio Community 2015中,選擇:文件–>新建–>項目–>模板–>JavaScript–>Apache Cordova Apps–>空白應用(Apache Cordova)–>名稱命名爲:HelloCordova–>肯定android

hellocordova

運行程序(Android):apache

打開Visual Studio Emulator for Android
選擇一個Android模擬器,如:VS Emulator 5" KitKat(4.4) XXHDPI Phone
啓動這個模擬器, 而後點擊運行:windows

startavd

run_hellocordova_in_Android

運行結果oracle

run_hellocordova_in_Android_result

運行程序(Windows Phone)app

Windows Phone:選擇Windows Phone(Universal), 選擇一個模擬器,如:Mobile Emulator 10.0.10586.0 WVGA 4 inch 1GB, 點擊運行:ide

run_hellocordova_in_WindowsPhone

運行結果

run_hellocordova_in_WindowsPhone_result

拍照

拍照功能須要額外下載插件,Visual Studio Community 2015提供了很是方便的插件下載安裝機制, 在HelloCordova這個項目中,點擊config.xml這個文件,
選擇:插件–>核心–>Camera–>並點擊添加按鈕,便可把插件加到當前項目中。

add_comera_plugin

代碼清單

/HelloCordova/www/index.html

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=gb2312"/>
        <meta http-equiv="Content-Security-Policy" content="default-src 'self' data: gap: https://ssl.gstatic.com 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *">
        <meta name="format-detection" content="telephone=no">
        <meta name="msapplication-tap-highlight" content="no">
        <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width">
        <link rel="stylesheet" type="text/css" href="css/index.css">
        <title>HelloCordova</title>
    </head>
    <body>
        <div>
            <h1 style="color:white">Take Photo</h1>
        </div>
        <div>
            <input id="btnTakePhoto" type="button" value="Take Photo" />
        </div>
        <div id="divPic"></div>


        <script type="text/javascript" src="cordova.js"></script>
        <script type="text/javascript" src="scripts/platformOverrides.js"></script>
        <script type="text/javascript" src="scripts/index.js"></script>
    </body>
</html>

/HelloCordova/www/scripts/index.js

(function () {
    "use strict";

    document.addEventListener( 'deviceready', onDeviceReady.bind( this ), false );

    function onDeviceReady() {
        
        document.addEventListener( 'pause', onPause.bind( this ), false );
        document.addEventListener( 'resume', onResume.bind( this ), false );
        
        document.getElementById('btnTakePhoto').onclick = function () {
            navigator.camera.getPicture(function (imageURI) {
                var pic = document.getElementById('divPic');
                pic.innerHTML = "<img src= '" + imageURI + "'/>";
            }, null, null);
        };
    };

    function onPause() {
        // TODO: 
    };

    function onResume() {
        // TODO: 
    };
} )();

運行結果(Android):

take_photo_in_android

運行結果(Windows Phone):

take_photo_in_windowsphone


更多:http://cordova.apache.org/

相關文章
相關標籤/搜索