Xamarin.iOS開發初體驗

來源:互聯網
上載者:User

標籤:


Xamarin是一個跨平台開發架構,這一架構的特點是支援用C#開發IOS、Android、Windows Phone和Mac應用,這套架構底層是用Mono實現的。


Mono是一款基於.NET架構的開源工程,包含C#語言編譯器、CLR運行時和一組類庫,能運行於Windows、Linux、Unix、Mac OS和Solaris。
對於.NET程式員來說,Xamarin是走向安卓、iOS、Mac跨平台開發的神器,不僅能用熟悉的C#來開發,還能使用Visual Studio作為IDE。本文內容是Xamarin.iOS版本的開發初體驗。

一.安裝開發環境

對於用Xamarin來開發iOS應用來說,準備工作分兩步:(1)在Windows下安裝Xamarin.iOS SDK以及VS,(2)在Mac下安裝Xamarin.iOS.Build Host以及XCode。因為Xamarin iOS for Visual Studio允許IOS應用在Windows下編寫,但只能在遠程Mac主機或iOS裝置上進行部署和調試。
1.Windows
在Windows系統下,首先確保Xamarin官網下載Xamarin完整的安裝嚮導,安裝嚮導會提供IOS以及Android套件的安裝,可自行選擇。

我在使用安裝嚮導安裝Xamarin時,一直卡在這裡:


在擷取Android和iOS可選套件的過程一直連結失敗,一開始以為是系統版本問題(我是Win8.1 64位),跟一些高手交流之後發現是這些資源被牆掉了。。。FQ自行解決。我還跟客服聯絡得到回複如下:

There are a couple possible causes of the issue, but it looks like a manual uninstall/reinstall would get everything working properly.
1) Press the Windows button + r to open the Run window.
2) Type appwiz.cpl to open the Add & Remove programs window.
3) Find Xamarin & Xamarin Studio (Optional) and uninstall them.
4) Once they’re uninstalled, or if you’re unable to find them; please follow the installation steps below.

Please download and install the packages shown below in the order listed:

1. JDK 1.6: http://download.xamarin.com/Installer/MonoForAndroid/jdk-6u39-windows-i586.exe
2. Android SDK: http://dl.google.com/android/installer_r22-windows.exe
3. GTK#: http://download.xamarin.com/GTKforWindows/Windows/gtk-sharp-2.12.25.msi
4. Xamarin Studio: http://download.xamarin.com/studio/Windows/XamarinStudio-5.0.0.878-0.msi
5. Xamarin for Windows (includes Xamarin.iOS for Visual Studio and Xamarin.Android): http://download.xamarin.com/XamarinforVisualStudio/Windows/Xamarin.VisualStudio_Setup-3.0.54.0.msi

I hope this helps!
-Kent Green
Developer Support

忽略Android SDK/JDK和Xamarin Studio,GTK#是對跨平台圖形化使用者介面庫GTK+的封裝,Windows使用者可以忽略,它會隨著Mono一起安裝,在非Windows平台上GTK#類似於Windows表單和WPF。所以只需要單獨安裝一個Xamarin for Windows就可以了。最新版本的Xamarin for Windows支援VS2012和VS2013,請戳這裡

安裝完成之後開啟VS2013,建立項目視窗可以看到多了iOS和Android目錄,表明安裝成功:

2.Mac
Xamarin.iOS支援Mac OS 10.7及以上版本。

首先到iOS Dev Center下載Xcode 5

接下來可以選擇安裝完整的Xamarin,或者只安裝Xamarin.iOS Build Host

二.配置部署調試環境

由於Windows下沒有iOS模擬器,因此需要連結一台連網的遠程Mac主機。除了上一節安裝了Xcode和Xamarin.iOS外,還需要對網路進行配置。
在Mac裡,在網路喜好設定裡,將當前網路的代理啟用並相應設定好,同時在系統喜好設定->安全性與隱私 中將防火牆關閉。

開啟Xamarin.iOS Build Host:

點擊配對自動產生一串數字 PIN 碼,在Windows下的VS設定中使用:

回到Windows,開啟VS2013,首先會自動讓我們進行帳號登入,所使用的帳號即你在Xamarin官網下載時需要建立的帳號,我這裡使用的都是試用版License,有能力就付費使用正式版吧;當然免費版也可以用,只不過在發布程式的時候有大小限制。

在菜單Tools->Options->Xamarin->iOS Settings選項卡,點擊Find Mac Build Host:



輸入在Mac下產生的數字 PIN 碼,完成配對,這樣Windows便和Mac主機建立遠端連線,可以在Windows下遠端偵錯iOS程式了。

三.建立一個iOS應用

開啟VS2013,建立一個項目,選擇iOS->iPhone->Blank App模板,命名為HelloOpenXLive,可以看到工程結構如下:

可以看到引用節點中的monotouch,即為Xamarin.iOS SDK,用.NET實現了iOS的基礎功能。

Main.cs為程式的入口,啟動一個繼承自UIApplicationDelegate的名為AppDelegate,在AppDelegate中完成程式的啟動和初始化工作。

AppDelegate.cs中,在FinishedLaunching方法裡,我們可以通過表單的RootViewController添加我們想要的畫面(View),這個結構有點類似於MVC模式。我們右鍵項目->添加項,添加一個iPhone View Controller,命名為HelloUIViewController:

開啟HelloUIViewController.cs:

1 public override void ViewDidLoad()
2 {
3     View = new UniversalView();
4  
5     base.ViewDidLoad();
6 }

首先添加了一個通用的視圖,即背景視圖,可以任意修改預設背景顏色,在UniversalView類的Initialize方法中

1 void Initialize()
2 {
3     BackgroundColor = UIColor.White;
4 }

目前為止畫面還是一片空白,我們回到HelloUIViewController類的ViewDidLoad方法,添加一個Label,顯示一段文字:

01 public UILabel CustomLabel;
02  
03 public override void ViewDidLoad()
04 {
05     View = new UniversalView();
06  
07     base.ViewDidLoad();
08  
09     // Perform any additional setup after loading the view
10     CustomLabel = new UILabel(new RectangleF(0, 100, 300, 30))
11     {
12         Text = "Hello, OpenXLive!",
13         TextAlignment = UITextAlignment.Center,
14     };
15     View.Add(CustomLabel);
16 }

在工具列中,設定好調試選項,選擇DEBUG、iPhoneSimulator,以及指定一種模擬器類型(如iPhone Retina(4-inch) iOS 7.1),按F5開始調試

在Mac主機下,可以看到一個iOS模擬器被啟動並初始化,我們的程式被部署在模擬器之後啟動,接下來就可以進行斷點調試了:

千萬不要吐槽為什麼沒有iPhone的外殼,這是因為所使用的Mac主機比較老,沒有Retina螢幕,解析度跟不上,所以水果殼沒顯示出來;如果解析度夠了自動就顯示出來了。

下一篇我將介紹如何通過Xamarin移植一個.NET的第三方庫到iOS上(iOS通過Xamarin調用OpenXLive)

總結

本文介紹了基於Mono的Xamarin架構,支援使用C#開發iOS、android、Windows Phone和Mac應用;介紹了Xamarin.iOS的安裝和配置方法;介紹了如何使用Xamarin.iOS SDK在Windows下進行開發,並且在Mac遠程主機上進行調試的過程。

Xamarin.iOS開發初體驗

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.