- Xamarin 教程
- Xamarin - 首頁
- Xamarin - 安裝
- Xamarin - 第一個應用程式
- Xamarin - 應用程式清單
- Xamarin - Android 資源
- Xamarin - Android 活動生命週期
- Xamarin - 許可權
- Xamarin - 構建應用程式 GUI
- Xamarin - 選單
- Xamarin - 佈局
- Xamarin - Android 小部件
- Xamarin - Android 對話方塊
- Xamarin - 相簿
- Xamarin - Android 檢視
- Xamarin - 多螢幕應用程式
- Xamarin - 部署您的應用程式
- Xamarin 有用資源
- Xamarin - 快速指南
- Xamarin - 有用資源
- Xamarin - 討論
Xamarin - 第一個應用程式
本章,我們將學習如何使用 Xamarin 建立一個小型 Android 應用程式。
Hello Xamarin! 應用程式
首先,啟動 Visual Studio 的新例項,然後轉到 **檔案 → 新建 → 專案**。
在出現的選單對話方塊中,轉到 **模板 → Visual C# → Android → 空白應用 (Android)**。
為您的應用程式提供一個合適的名稱。在本例中,我們將其命名為 **“helloWorld”** 並將其儲存在提供的預設位置。接下來,單擊“確定”按鈕以載入新的 **“helloXamarin”** 專案。
在 **解決方案** 中,開啟 **資源 → 佈局 → Main.axml** 檔案。從設計檢視切換到 **源** 檔案,並鍵入以下程式碼行以構建您的應用程式。
<?xml version = "1.0" encoding = "utf-8"?>
<LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"
android:orientation = "vertical"
android:background = "#d3d3d3"
android:layout_width = "fill_parent"
android:layout_height = "fill_parent">
<TextView
android:text = "@string/HelloXamarin"
android:textAppearance = "?android:attr/textAppearanceLarge"
android:layout_width = "match_parent"
android:layout_height = "wrap_content"
android:id = "@+id/textView2"
android:textColor = "@android:color/black" />
</LinearLayout>
在上面的程式碼中,我們建立了一個新的 Android **textview**。接下來,開啟資料夾 values 並雙擊 **Strings.xml** 開啟它。在這裡,我們將儲存關於上面建立的 **button** 的資訊和值。
<?xml version = "1.0" encoding = "utf-8"?> <resources> <string name = "HelloXamarin">Hello World, I am Xamarin!</string> <string name = "ApplicationName">helloWorld</string> </resources>
開啟 **MainActivity.cs** 檔案,並將現有程式碼替換為以下程式碼行。
using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
namespace HelloXamarin {
public class MainActivity : Activity {
protected override void OnCreate(Bundle bundle) {
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
}
}
}
儲存應用程式。構建並執行它以在 Android 模擬器中顯示建立的應用程式。
如果您沒有 Android 模擬器,請按照下一節中給出的步驟建立一個。
設定 Android 模擬器
在 Visual Studio 選單上,轉到 **工具 → Android → Android 模擬器管理器**。在出現的彈出視窗中,單擊 **建立** 按鈕。它將顯示以下螢幕。
在上面的螢幕上,提供您想要的 **AVD 名稱**。選擇適合您顯示器的 **裝置**,例如 Nexus 4” 顯示器。選擇您的 **目標平臺**。建議始終在最低目標平臺(例如,API 10 Android 2.3(薑餅))上進行測試,以確保您的應用程式可在所有 Android 平臺上執行。
填寫其餘欄位並單擊“確定”按鈕。您的模擬器現在已準備就緒。您可以從現有 Android 虛擬裝置列表中選擇它,然後單擊 **啟動** 以啟動它。
修改 HelloXamarin 應用程式
在本節中,我們將修改我們的專案並建立一個按鈕,該按鈕將在單擊時顯示文字。開啟 **main.axml** 並切換到 **原始碼檢視**。在我們建立的 **textview** 之後,我們將新增一個按鈕,如下所示。
<Button android:id = "@+id/MyButton" android:layout_width = "fill_parent" android:layout_height = "wrap_content" android:text = "@string/ButtonClick" />
新增按鈕後,我們的完整程式碼如下所示:
<?xml version = "1.0" encoding = "utf-8"?>
<LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"
android:orientation = "vertical"
android:layout_width = "fill_parent"
android:layout_height = "fill_parent">
<TextView
android:text = "@string/HelloXamarin"
android:textAppearance = "?android:attr/textAppearanceLarge"
android:layout_width = "match_parent"
android:layout_height = "wrap_content"
android:id = "@+id/textView2" />
<Button
android:id = "@+id/MyButton"
android:layout_width = "fill_parent"
android:layout_height = "wrap_content"
android:text = "@string/ButtonClick" />
</LinearLayout>
接下來,我們在 **strings.xml** 檔案中註冊我們的按鈕值。
<string name = "ButtonClick">Click Me!</string>
在 **strings.xml** 檔案中新增按鈕後,我們將開啟 **MainActivity.cs** 檔案,為單擊按鈕時新增操作,如下面的程式碼所示。
using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
namespace HelloXamarin {
[Activity(Label = "HelloXamarin", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity {
protected override void OnCreate(Bundle bundle) {
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
Button button = FindViewById<Button>(Resource.Id.MyButton);
button.Click += delegate { button.Text = "Hello world I am your first App"; };
}
}
}
接下來,構建並執行您的應用程式。
單擊按鈕後,您將獲得以下輸出: