[轉]Cordova + Ionic in Visual Studio - 101 Tutorial [Part I]

本文轉自:http://binarylies.ghost.io/cordova-ionic-in-visual-studio/

?

Hi everyone,

I thought about lending a hand to all the people that are, as I have been recently, exploring Cordova and all its features for the first time.

For the ones still trying to figure out what Apache Cordova is, I'll try to briefly explain it to you.

Apache Cordova

Apache Cordova (or Adobe PhoneGap) is a platform that provides hybrid mobile web development technology so you can build native mobile applications using HTML, CSS and JavaScript. Basically, Cordova provides a native application hosting a full screen Web View where you'll develop your web app.

Alongside this, Cordova provides a plugin Javascript API that allows you to access native features and API's. Currently there are hundreds of plugins available for you to use or, of course, you can build your own. The compatibility of the existing plugins may vary, but most of them are available to the iOS and Android platforms.

Cordova for Visual Studio

Recently Microsoft released Visual Studio Tools for Apache Cordova. With these tools you can integrate your development process from coding and building to debugging in this powerful IDE.

Clarifying the last point, debug in Visual Studio is available for Android (>= 4.4), iOS (it provides a remote iOS build and debugging agent that interfaces with XCode’s command line build tools - a Mac and Apple Developer Account is required to build and debug) and apparently with the most recent update to this toolset it is also possible to debug Windows Phone 8.1.

Let me be clear: both Apache Cordova and Cordova Tools for Visual Studio still have [a lot of] limitations. I've banged my head against the wall figuring out a way to work around some issues. However, this will not be the subject of today's post, and let's admit, having a platform, a free one, that allows you to write code once and publish it for several devices, and still manage to keep the mobile touch and feel is pretty cool, even with a few shortcomings.

Requirements

As you have certainly understood by now, my demonstration will be based on Visual Studio, but if you're using the command line, you can follow this too. You just have to replace the visual studio actions with the respective cordova commands.

So, this is what I'm gonna use to show you a basic cordova project:

  • HTML5 & JavaScript (makes sense, right? if you don't know them, you should probably stop here, and come back when you do)
  • AngularJS (you don't need to be a pro - I'm certainly not - but you should have a basic knowledge of it)
  • Ionic? (Open source library of? HTML, CSS and JS components, gestures, and tools optimized for mobile)
  • Bower (You should have nodejs + npm installed to install bower)

If you're using Visual Studio, these are your requirements:

  • Visual Studio 2013 + Update 4
  • Visual Studio Tools for Apache Cordova CTP3.1

or

  • Visual Studio 2015 CTP6

Ready, Set, Let's Go!

First, you've to create your blank project. If you're using Visual Studio (after you install everything required), you'll have to go to: File > New > Project > And then look for the Apache Cordova Apps templates under JavaScript.

Visual Studio Apache Cordova Apps template

Then, and I remind you that you should have nodejs + npm + bower installed, you're going to open a command line inside your project's folder and do > bower init. Here you may just keep the defaults, by pressing enter a few times. At this point we have our project initiated. But pretty empty. Your project should look like the this:

Project structure on Visual Studio

Let's install ionic now, using bower:?

> bower install driftyco/ionic-bower#master --save

And after installing, we're adding ionic references to our index.html. I'll be adding ionic.bundle, which includes Ionic scripts as well as AngularJS.

    <!-- BinaryLiesApp references --><link href="bower_components/ionic/css/ionic.css" rel="stylesheet" /> <!-- (...) --> <script src="bower_components/ionic/js/ionic.bundle.js"></script> 

Right now, we should have all we need to run our app (Yey!). We should show something more interesting than the default Hello, your application is ready! text though. Let's put ionic to work, and add a header. Your index.html should look like this for the moment:

<!DOCTYPE html>  
<html> <head> <meta charset="utf-8" /> <title>BinaryLiesApp</title> <!-- BinaryLiesApp references --> <link href="bower_components/ionic/css/ionic.css" rel="stylesheet" /> <link href="css/index.css" rel="stylesheet" /> </head> <body> <div class="bar bar-header bar-positive"> <h1 class="title">Binary Lies</h1> </div> <!-- Cordova reference, this is added to your app when it's built. --> <script src="cordova.js"></script> <script src="scripts/platformOverrides.js"></script> <script src="bower_components/ionic/js/ionic.bundle.js"></script> <script src="scripts/index.js"></script> </body> </html> 

If you try to run it, you should see something like the next image. I used ripple in this case, which is another tool that comes with Visual Studio Tools for Cordova, but you can always run it directly to your device.

App state by the moment

Ok guys, now that we have it all running, let's start creating our app shall we? We start by doing what we would do in any other web app, build it from the ground up!

I'll be using angular, which binds very well with ionic. You can use whatever structure you feel more comfortable with. As for me, I started by creating a folder app where I'm gonna put all my angular related files, i.e. my controllers, services, directives and template files. It looks like this: App angular structure

In the app.js file, besides registering my services and controllers, and defining my routes, I'm gonna inject the ionic service.

var app = angular.module('BinaryLiesApp', ['ionic', 'angularMoment']); 

I'm doing one more thing in here. I'm going to manually initiate angular, only when the device is ready.

Why am I doing this? Well, further ahead we're gonna use some device services that are only available after the device is ready. So, to avoid having some troubles trying to use services not yet available, you can either have a specific service that you call to check if the device is ready for you to use, and this is probably the most efficient way to do this, or... you can bootstrap your app only after the device ready event fires. For what I'm going to show you in this post, this approach will be enough.

var onDeviceReady = function () { console.log("Application start"); angular.bootstrap(document.body, ['BinaryLiesApp']); } document.addEventListener('deviceready', onDeviceReady.bind(this), false); 

In my routes, I set up an abstract state using menu.html template. This will be the main template and AppController will be the parent controller:

.state('app', { url: "/app", abstract: true, templateUrl: "app/templates/menu.html", controller: 'AppController' }) 

So let's draw the menu using Ionic, namely ionic directives. The side menu directive goes like this:

<ion-side-menus>  <ion-side-menu-content> <!-- 01. Here goes the view content --> </ion-side-menu-content> <ion-side-menu side="left" expose-aside-when="large"> <!-- 02. While in here goes the menu contents --> </ion-side-menu> </ion-side-menus> 

In the menu zone (02.), you can see that it's possible to define the menu side and there's another attribute there, expose-aside-when, to keep the side menu open when you're accessing the app from a 'large' device. Useful for tablets for instance. Inside this area, we'll draw a header - using an ionic angular directive - and the content of the menu, which is a list of menu options.

<header class="bar bar-positive menu-header bar-header bar-stable"> <!-- Here is our positive header, with our title inside--> <h2>Binary Lies</h2> </header> <ion-content class="has-header menu-header"> <!-- Here we have a ion-content directive. You'll see it more times with a list of items. These items have an 'item-icon-left' class, so they adjust to the ionicon that we're adding to each item--> <ion-list> <ion-item nav-clear item-icon-left menu-close href="#/app/posts"> <i class="icon ion-ios-paper"></i> Blog Posts </ion-item> <ion-item nav-clear item-icon-left menu-close href="#/app/favorites"> <i class="icon ion-ios-heart"></i> Favorites </ion-item> </ion-list> </ion-content 

Back to the view contents (01.). First, we should have a navigation bar that will hold our title and some buttons for quick actions, and then the container for the views.

<ion-nav-bar class="bar bar-positive nav-title-slide-ios"> <!-- Here again our positive bar. And back button (required for the iOS platforms --> <ion-nav-back-button class="button-clear"><i class="icon ion-ios-arrow-back horizontal-padd-5"></i></ion-nav-back-button> <ion-nav-buttons side="right"> <!-- And some buttons, on the right side of the nav-bar --> <button class="button button-icon icon ion-ios-search-strong"> </button> <button class="button button-icon icon ion-android-more-vertical"></button> </ion-nav-buttons> </ion-nav-bar> <!-- And down here we have the container for the view content --> <ion-nav-view name="menuContent" animation="slide-left-right"></ion-nav-view> 

This is our menu, but now we have to show some content views. Our default view (app.js) is going to be a list of blog posts.

.state('app.posts', { url: "/posts", views: { 'menuContent': { templateUrl: "app/templates/posts.html", controller: 'PostsController' } } }); $urlRouterProvider.otherwise('/app/posts'); 

Our PostsController.js will be fed by a PostsService.

var PostsService = ['$http', '$q', function ($http, $q) { var self = this; self.Posts = []; this.GetPosts = function () { // You would probably want to make a $http request here to obtain your data self.Posts = [ { Id: 1, Title: "Sinatra Tests 101 - RSpec", Date: new Date("2015-03-16"), Author: "Tiago Casanova", ImageUrl:"http://blog.softartisans.com/wp-content/uploads/2012/03/sinatraruby.jpg" }, { Id: 2, Title: "Sinatra Tests 101 - MiniTest", Date: new Date("2015-03-31"), Author: "Tiago Casanova", ImageUrl:"http://blog.softartisans.com/wp-content/uploads/2012/03/sinatraruby.jpg" }, { Id: 3, Title: "Cordova + Ionic in Visual Studio - 101 Tutorial [Part I]", Date: new Date("2015-04-20"), Author: "Joana Barbosa", ImageUrl: "https://xrgqhq.dm2302.livefilestore.com/y2pFCNt436TcSShqnPwputyXorHc1Szo8rwrJJzMXp9NWUWYFvGtm3BlG95ohv2MvaCHJo63vD87xgJiu7fw_aNmzR9lceKyXymu7f3ZUFXP4dd2XS41CN-celCPvaJ1wDogkcicT0Q1Es07g6O_o7iHw/cordova_p1_header.png?psid=1" }, { Id: 4, Title: "Sinatra Tests 101 - Cucumber", Date: new Date("2015-04-27"), Author: "Tiago Casanova", ImageUrl: "http://tinyurl.com/ppms8cu" } ]; return self.Posts; } }]; 

For today's demo, I'm using local static data. Of course, this should be replaced by your http request.

My controller will also be very simple.?

"use strict";
var PostsController = ['$scope', 'postsService', function ($scope, postsService) { var self = this; $scope.model = { posts: [] }; self.Init = function () { $scope.model.posts = postsService.GetPosts(); } self.Init(); }]; 

By now, I already have all data available to show in my view. Let's use ionic to display it.?

<ion-view title="Posts"> <ion-nav-buttons side="left"> <!-- Here we're adding a button to toggle the side menu--> <button menu-toggle="left" class="button button-icon icon ion-navicon"></button> </ion-nav-buttons> <ion-content class="center" ng-show="model.posts.length==0"> <!-- 03. Loading content--> <div class="row"> <ion-spinner></ion-spinner> </div> </ion-content> <ion-content class="has-header" ng-hide="model.posts.length==0"> <!-- 04. List of posts--> <ion-list> <ion-item ng-repeat="item in model.posts" class="item-thumbnail-left" href="#/app/post/{{item.Id}}"> <img ng-src="{{item.ImageUrl}}"> <h2>{{item.Title}}</h2> <p>{{item.SubTitle}}</p> <p>{{item.Date | amCalendar}}</p> </ion-item> </ion-list> </ion-content> </ion-view> 

I added the first ion-content (03.) to show an ionic-spinner while the user is waiting for the posts' data to be loaded. And after that one, we got our post list(04.). We're using an ionic class to present the list with a left thumbnail while also using an angular-moment filter to display the post date.

This post is getting too big, so I'm stopping here. By now you should have an app with a left-side menu, with one view, presenting the list of the posts.

It should look like this. Not bad, right? Final look

We've built our basic skeleton but this was just the start. We haven't messed up with cordova plugins to interact with the device yet! I promise I'll show you more of that in the next part of this tutorial.

Thank you for reading.

Take care! See you in the next one ;)

Joana Barbosa

Junior BI & Web Developer @ Devscope, learning to knock data out using BI! As a web enthusiast, I'm always interested in exploring new Web tools and technologies.

Porto, Portugal???????????????????? https://twitter.com/JoanaVBarbosa

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/286605.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/286605.shtml
英文地址,請注明出處:http://en.pswp.cn/news/286605.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

【轉】學會這13個原則寫UI界面文案,用戶才能秒懂

原文網址&#xff1a;http://www.niaogebiji.com/article-12011-1.html 摘要: 首先&#xff0c;在寫UI文案之前&#xff0c;為了理清思路&#xff0c;要先搞清楚三個問題&#xff1a;我&#xff08;設計師&#xff09;想讓用戶做什么&#xff1f; – 我寫文案的目的為什么要讓用…

【前端就業課 第一階段】HTML5 零基礎到實戰(一)基礎代碼結構詳解

注意&#xff1a;手機&#xff08;APP&#xff09;打開&#xff0c;內容顯示更佳&#xff0c;不會的私聊博主即可 想要拿代碼或加入學習計劃&#xff08;** 博主會監督你并且教你寫文章 **&#xff09;的拉到最下面&#xff08;PC端Web打開&#xff09;加博主即可&#xff0c;目…

【框架篇】mvc、mvp、mvvm使用關系總結

MVC MVC全名是Model View Controller&#xff0c;是模型(model)&#xff0d;視圖(view)&#xff0d;控制器(controller)的縮寫&#xff0c;一種軟件設計典范&#xff0c;用一種業務邏輯、數據、界面顯示分離的方法組織代碼&#xff0c;將業務邏輯聚集到一個部件里面&#xff0c…

Blazor University (26)路由 —— 通過代碼導航

原文鏈接&#xff1a;https://blazor-university.com/routing/navigating-our-app-via-code/通過代碼導航源代碼[1]從 Blazor 訪問瀏覽器導航是通過 NavigationManager 服務提供的。這可以使用 razor 文件中的 inject 或 CS 文件中的 [Inject] 屬性注入到 Blazor 組件中。Navig…

Android之使用自定義華為掃描SDK掃描二維碼和識別本地圖片

1、需求 更具UI的設計實現掃描二維碼和識別本地圖片二維碼功能。 zxing掃描二維碼還可以,但是識別本地圖片二維碼功能效果太差,非常不理想,看了看多github很多zxing擴展的開源項目,識別本地圖片二維碼功能效果不樂觀,有些甚至還有裁剪本地圖片,還是不能識別,果斷放棄zb…

Matlab R2016b簡體中文版安裝教程(附Matlab R2016b百度網盤下載地址)

下載的Matlab R2016b軟件安裝包(文末附有下載地址)目錄如下所示: 安裝過程: 1. 安裝主程序R2016b_win64_dvd1.iso和R2016b_win64_dvd2.iso 由于目前大多數及其都是Win8或10系統,所以選中R2016b_win64_dvd1.iso,右鍵→Windows資源管理器打開。Win7系統可以安裝好壓軟件之后…

深度學習String、StringBuffer、StringBuilder

相信String這個類是Java中使用得最頻繁的類之一&#xff0c;并且又是各大公司面試喜歡問到的地方&#xff0c;今天就來和大家一起學習一下String、StringBuilder和StringBuffer這幾個類&#xff0c;分析它們的異同點以及了解各個類適用的場景。下面是本文的目錄大綱&#xff1a…

Leetcode之打印鏈接的倒數第K個節點

1 問題 打印鏈表倒數第K個節點值。 2 代碼實現 #include<stdio.h>//定義一個Node結構體,里面包含了value值和保存了下一個Node的指針(地址) typedef struct Node {int value;Node *next; } Node;//打印鏈表所有的值,循環遍歷一次,因為鏈表節點的最后一個節點肯定是…

MySQL5.5多實例編譯安裝——mysqld_multi

一、MySQL多實例簡介 MySQL多實例&#xff0c;簡單地說&#xff0c;就是在一臺服務器上同時開啟多個不同的服務端口&#xff08;如&#xff1a;3306、3307&#xff09;&#xff0c;同時運行多個MySQL服務進程&#xff0c;這些服務進程通過不同的socket監聽來自不同的端口來提供…

【前端就業課 第一階段】HTML5 零基礎到實戰(二)超鏈接

注意&#xff1a;手機&#xff08;APP&#xff09;打開&#xff0c;內容顯示更佳&#xff0c;不會的私聊博主即可 想要拿代碼或加入學習計劃&#xff08;** 博主會監督你并且教你寫文章 **&#xff09;的拉到最下面&#xff08;PC端Web打開&#xff09;加博主即可&#xff0c;目…

架構,框架,模式,模塊、組件、插件的含義和區別

架構、框架、模式、模塊、組件、插件、控件、中間件的含義和區別。經常看到這些概念&#xff0c;但是有些含糊&#xff0c;花點兒功夫整理一下&#xff0c;結果還是有些地方理解的不透徹&#xff0c;先將整理的內容寫下來&#xff0c;以供交流。左側英文欄中有些單詞被分成了兩…

C語言,C#語言求100-999內的水仙花數源程序

水仙花數&#xff08;Narcissistic number&#xff09;也被稱為超完全數字不變數&#xff08;pluperfect digital invariant, PPDI&#xff09;、自戀數、自冪數、阿姆斯壯數或阿姆斯特朗數&#xff08;Armstrong number&#xff09;&#xff0c;水仙花數是指一個 3 位數&#…

一個精簡的C#表達式執行框架Dynamic Expresso

一、簡介Dynamic Expresso是一個用.NET Standard 2.0編寫的簡單c#語句的解釋器。Dynamic Expresso嵌入了自己的解析邏輯&#xff0c;通過將其轉換為.NET lambda表達式或委托來解釋c#語句。使用Dynamic Expresso開發人員可以創建可編寫腳本的應用程序&#xff0c;無需編譯即可執…

算法馬拉松13 A-E解題報告

A題意&#xff08;取余最長路&#xff09;: 佳佳有一個n*m的帶權矩陣&#xff0c;她想從(1,1)出發走到(n,m)且只能往右往下移動&#xff0c;她能得到的娛樂值為所經過的位置的權的總和。 有一天&#xff0c;她被下了惡毒的詛咒&#xff0c;這個詛咒的作用是將她的娛樂值變為對p…

Modis數據處理工具:MRT百度網盤下載和手把手圖文安裝教程

如下圖所示為 MODIS Reprojection Tool(MRT)的軟界面,看似簡單,卻是Modis遙感影像必不可少的處理工具,如投影變換等。本文以圖文并茂的形式,詳細講解MRT軟件在Windows10平臺上的安裝過程,并附MRT軟件百度網盤下載地址。 Modis Tool主界面: 一、安裝過程 1、安裝Jav…

Word中如何設置圖片與段落的間距為半行

第一種&#xff1a; 正文為5號&#xff0c;那么圖片或者Viso對象前后空一行&#xff0c;設置字號為7號或者更小&#xff0c;這樣設置的間距就是那個7號字的間距&#xff0c;比5號小&#xff0c;看著空白不是那么大。 第二種&#xff1a; Visio對象轉為jpg&#xff0c;然后選中圖…

在微信小程序中使用“隨機鍵盤”

最近研究微信小程序&#xff0c;發現在手機上使用系統鍵盤非常不方便&#xff0c;一是按鍵太小&#xff0c;對于小學生來說&#xff0c;操作非常不方便&#xff1b;二是系統鍵盤反復切換影響界面布局。于是自己決定自己寫一個隨機的小鍵盤。 原理非常簡單&#xff1a;拿“口算練…

Android之提示訂閱配置訂閱需要傳新的包 添加結算權限。

1 問題 apk上google應用市場&#xff0c;然后開通支付商品&#xff0c;錯誤提示如下 2 解決辦法 AndroidManifest.xml里面添加谷歌支付權限 <!-- google pay --><uses-permission android:name"com.android.vending.BILLING" />

【前端就業課 第一階段】HTML5 零基礎到實戰(三)一篇文CSS基礎入門

注意&#xff1a;手機&#xff08;APP&#xff09;打開&#xff0c;內容顯示更佳&#xff0c;不會的私聊博主即可 想要拿代碼或加入學習計劃&#xff08;** 博主會監督你并且教你寫文章 **&#xff09;的拉到最下面&#xff08;PC端Web打開&#xff09;加博主即可&#xff0c;目…

C#如何獲取實體類屬性名和值?

數據模型定義public class User{public User(){student new student();}public string name { get; set; }public string gender { get; set; }public int age { get; set; }public student student { get; set; }}public class student{public int ID { get; set; }public st…