現在我們來進行下一個階段,將是針對 Xamarin.iOS 的原生開發的練習,不過,在此之前,各位需要先行準備好您的 Visual Studio 2017 + Xamarin 開發環境;關於要如何準備您們的 Xamarin 開發環境,可以參考 第一次安裝 Visual Studio 2017 到新的作業系統上 或者 可以參考 Xamarin / Xamarin.Forms 行動跨平台 Mobile Cross-Platform 開發學習指引問答集 FAQ;若您還有 Xamarin 相關的問題,可以到 Xamarin.Forms @ Taiwan 與 Xamarin 同好一同進行討論。
關於使用 Xamarin 來開發 iOS 原生專案的相關基礎知識,可以參考 在 Windows 上安裝 Xamarin.iOS 文章,您將會有更深刻的認識。
您也可以查看微軟官方的 Xamarin 介紹文章 開始使用 iOS,來了解更多關於 Xamarin iOS 原生專案的開發技術與技巧。
建立一個 Xamarin iOS 專案
- 首先,我們打開 Visual Studio 2017 (任何一種版本 Visual Studio Community 2017 / Visual Studio Professional 2017 / Visual Studio Enterprise 2017皆可)
- 點選 Visual Studio 2017 功能表的 [檔案] > [新增] > [專案]
- 當出現 [新增專案] 對話窗,請依序選擇 [已安裝] > [Visual C#] > [iOS] > [Univseral] [單一檢視應用程式 (iOS)]
- 請在最下方 [名稱] 文字輸入盒欄位,輸入您想要的專案名稱
- 最後,請點選 [確定] 按鈕

- 一旦 Visual Studio 的方案 ( Solution ) , 專案 ( Project ) 建立完成之後,您可以從 方案總管 ( Solution Explorer ) 中,看到整個 Xamarin Android 的專案結構。

- 在 Resources 目錄下,可以存放您的應用程式頁面會用到資源與檔案,在這裡,您將會看到一個 LaunchScreen.xib 檔案,另外,我們也可以看到另外一個 [Main.storyboard] 檔案,我們將會使用這個檔案來進行應用程式畫面控制項的設計,更多這方面的資訊,可以參考 Interface Builder Built-In 或者 [Storyboard](https://developer.apple.com/library/content/documentation/General/Conceptual/Devpedia-CocoaApp/Storyboard.html) 。
- 在此之前,您先要確認您的 Windows Visual Studio 2017 已經成功與另外一台 Mac 電腦進行連線,您可以在工具列上看到一個電腦螢幕圖示,若這個圖示沒有呈現為綠色,則表示您的 Windows 電腦尚未成功與 Mac 電腦進行連線成功,這個時候,請點選這個螢幕圖示。

- 現在,您將會看到 [Pair to Mac Instructions] 對話窗,若您對於如何在 Mac 電腦上進行相關設定,才能夠讓 Windows Visual Studio 2017 可以成功連線,可以參考這個對話窗的三個說明頁面,而且,這個設定過程相當的容易。若您已經確認 Mac 電腦端已經設定成功之後,可以關閉這個對話窗。

- 現在,我們看到了另外一個對話窗,[Pair to Mac],若您尚未建立過任何連線,可以點選左下角的 [Add Mac] 按鈕,這個時候,會顯示出另外一個 [Add Mac] 對話窗,請在這個對話窗輸入您的 Mac 電腦 IP 位置,接著點選 [新增] 按鈕。最後,請點選右下角 [連線] 按鈕,開始進行與 Mac 電腦的連線。

- 如同前面所提到的,若可以成功與 Mac 電腦連線,此時,工具列上的螢幕圖示,就會變成綠色的囉。

- 現在,請從專案中,找到 [Main.storyboard] 檔案,使用滑鼠雙擊這個檔案,稍微等候一下,您將會看到如下圖的畫面,這裡就是我們要設計頁面呈現內容的地方,在左方,有工具箱視窗,這裡有各種 iOS 原生的控制項 ,您可以使用拖拉的方式,選擇您需要的控制項,拖拉到頁面上。

- 現在我們從工具列中,找到 [Button] 按鈕,拖拉到我們手機螢幕上。

- 請點選剛剛拖拉上的 [Button] 按鈕,此時,從 Visual Studio 的 Property 屬性視窗,可以看到一個 Name 欄位,請在這個欄位中輸入 MyButton 。

- 現在,請在方案總管中,找到 ViewController.cs 節點,使用滑鼠雙擊,打開這個檔案;因為我們使用 Xamarin iOS 原生專案開發的時候,使用的是 MVC 開發框架設計模式,因此,我們要針對這個頁面所要運行的商業邏輯,就可以在這個類別中設計各種 C# 程式碼;底下,是預設產生的程式碼內容。

public partial class ViewController : UIViewController
{
public ViewController(IntPtr handle) : base(handle)
{
}
public override void ViewDidLoad()
{
base.ViewDidLoad();
// Perform any additional setup after loading the view, typically from a nib.
}
public override void DidReceiveMemoryWarning()
{
base.DidReceiveMemoryWarning();
// Release any cached data, images, etc that aren't in use.
}
}
- 現在,我們需要修改 ViewController 這個類別,讓這個應用程式運作起來,如同前一個 Xamarin Android 文章中執行結果是相同的。我們現在加入了一行事件訂閱到 ViewDidLoad 方法中,這行程式碼說明了,當按下按鈕的時候,需要變更該按鈕的顯示文字內容。

public partial class ViewController : UIViewController
{
public int count { get; set; }
public ViewController(IntPtr handle) : base(handle)
{
}
public override void ViewDidLoad()
{
base.ViewDidLoad();
// Perform any additional setup after loading the view, typically from a nib.
MyButton.TouchUpInside += delegate { MyButton.SetTitle(string.Format("{0} clicks!", count++), UIControlState.Normal); };
}
public override void DidReceiveMemoryWarning()
{
base.DidReceiveMemoryWarning();
// Release any cached data, images, etc that aren't in use.
}
}
準備進行 Xamarin Android 專案的建置、執行、與除錯
- 現在,我們要來準備進行這個 iOS 應用程式的運行,由於我們要選擇在 Mac 電腦上的模擬器來運行,因此,請在工具列上選擇 [iPhoneSimlator] 與 想要執行的 SDK 版本,最後,按下 F5 來開始執行這個專案。

- 底下為在 iOS 模擬器上運行的畫面

進階研讀
您可以繼續來研讀 Xamarin 新手入門 Part 3 Xamarin.Forms 專案開發教學
了解更多關於 [Xamarin.Android] 的使用方式
了解更多關於 [Xamarin.iOS] 的使用方式
了解更多關於 [Xamarin.Forms] 的使用方式
了解更多關於 [Hello, Android:快速入門] 的使用方式
了解更多關於 [Hello, iOS – 快速入門] 的使用方式
了解更多關於 [Xamarin.Forms 快速入門] 的使用方式
Xamarin 新手入門 Part 1 Xamarin.Android 專案開發教學