建置您的第一個 Xamarin.Forms 應用程式

適用於 Windows 的逐步指示

Download Sample 下載範例

請遵循下列步驟及上面的影片進行:

  1. 選擇 [ 檔案 > 新 > 專案... ],或按 [ 建立新專案...] 按鈕。

  2. 搜尋 "Xamarin",或從 [專案類型] 功能表中選擇 [行動裝置]。 選取 [行動應用程式] 項目Xamarin.Forms類型。

  3. 選擇項目名稱 – 此範例使用 “AwesomeApp”。

  4. 按一下 [空白] 專案類型,並確定已選取 [Android] 及 [iOS]:

    Android and iOS Blank App

  5. 等候 NuGet 套件還原完成 (狀態列中將會顯示 [還原完成] 訊息)。

  6. 新的 Visual Studio 2022 安裝不會安裝 Android SDK,系統可能會提示您安裝最新的 Android SDK:

    Install Android SDK

  7. 新的 Visual Studio 2022 安裝不會設定 Android 模擬器。 按一下 [偵錯] 按鈕上的下拉箭號,然後選擇 [建立 Android Emulator] 以啟動模擬器建立畫面:

    Create Android Emulator dropdown

  8. 在模擬器建立畫面中,使用預設設定,然後按一下 [建立] 按鈕:

    Android emulator creation screen

  9. 建立模擬器會讓您回到 [裝置管理員] 視窗。 按一下 [開始] 按鈕,以啟動新的模擬器:

    Android emulator in the Device Manager

  10. Visual Studio 2022 現在應該會在 [ 錯] 按鈕上顯示新模擬器的名稱:

    Android emulator name on the Debug button

  11. 按一下 [偵錯] 按鈕,以建置應用程式並將其部署至 Android Emulator:

    Android emulator displaying the application

自訂應用程式

您可以自訂應用程式來新增互動式功能。 請執行下列步驟,以將使用者互動新增至應用程式:

  1. 編輯 MainPage.xaml,並在 </StackLayout> 結尾前面新增此 XAML:

    <Button Text="Click Me" Clicked="Button_Clicked" />
    
  2. 編輯 MainPage.xaml.cs,並將這段程式碼新增至類別結尾:

    int count = 0;
    void Button_Clicked(object sender, System.EventArgs e)
    {
        count++;
        ((Button)sender).Text = $"You clicked {count} times.";
    }
    
  3. 偵錯 Android 上的應用程式:

    Android app with Button

在 Visual Studio 2022 中建置 iOS 應用程式

您可以使用具有網路連線的 Mac 電腦,從 Visual Studio 建置 iOS 應用程式並進行偵錯。 如需詳細資訊,請參閱安裝指示

適用於 Windows 的逐步指示

Download Sample 下載範例

請遵循下列步驟及上面的影片進行:

  1. 選擇 [ 檔案 > 新 > 專案... ],或按 [ 建立新專案...] 按鈕:

    Create a new project

  2. 搜尋 "Xamarin",或從 [專案類型] 功能表中選擇 [行動裝置]。 選取行動 應用程式 (Xamarin.Forms) 項目類型:

    Filter for Xamarin projects

  3. 選擇項目名稱 – 此範例使用 「AwesomeApp」:

    Choose a project name

  4. 按一下 [空白] 專案類型,並確定已選取 [Android] 及 [iOS]:

    Android and iOS, with .NET Standard

  5. 等候 NuGet 套件還原完成 (狀態列中將會顯示 [還原完成] 訊息)。

  6. 新 Visual Studio 2019 安裝將不包含已設定的 Android Emulator。 按一下 [偵錯] 按鈕上的下拉箭號,然後選擇 [建立 Android Emulator] 以啟動模擬器建立畫面:

    Create Android Emulator dropdown

  7. 在模擬器建立畫面中,使用預設設定,然後按一下 [建立] 按鈕:

    Android emulator creation screen

  8. 建立模擬器會讓您回到 [裝置管理員] 視窗。 按一下 [開始] 按鈕,以啟動新的模擬器:

    Android emulator in the Device Manager

  9. Visual Studio 2019 現在應該會在 [偵錯] 按鈕上顯示新模擬器的名稱:

    Android emulator name on the Debug button

  10. 按一下 [偵錯] 按鈕,以建置應用程式並將其部署至 Android Emulator:

    Android emulator displaying the application

自訂應用程式

您可以自訂應用程式來新增互動式功能。 請執行下列步驟,以將使用者互動新增至應用程式:

  1. 編輯 MainPage.xaml,並在 </StackLayout> 結尾前面新增此 XAML:

    <Button Text="Click Me" Clicked="Button_Clicked" />
    
  2. 編輯 MainPage.xaml.cs,並將這段程式碼新增至類別結尾:

    int count = 0;
    void Button_Clicked(object sender, System.EventArgs e)
    {
        count++;
        ((Button)sender).Text = $"You clicked {count} times.";
    }
    
  3. 偵錯 Android 上的應用程式:

    Android app

注意

此範例應用程式包含影片中未提及的其他互動式功能。

在 Visual Studio 2019 中建置 iOS 應用程式

您可以使用具有網路連線的 Mac 電腦,從 Visual Studio 建置 iOS 應用程式並進行偵錯。 如需詳細資訊,請參閱安裝指示

這段影片說明在 Windows 上,使用 Visual Studio 2019 建置及測試 iOS 應用程式的程序:

適用於 Mac 的逐步指示

Download Sample 下載範例

請遵循下列步驟及上面的影片進行:

  1. 選擇 [ 檔案 > 新方案... ] 或按 [ 新增專案...] 按鈕,然後選取 [多平臺 > 應用程式 > 空白窗體應用程式]:

    Blank Forms App

  2. 確定 已選取 AndroidiOS

    Android and iOS, with .NET Standard

注意

您的應用程式名稱和組織標識碼只支援 A-Z、a-z、'_'、'.' 和數位。

  1. 以滑鼠右鍵按一下方案來還原 NuGet 套件:

    Screenshot shows Restore NuGet Packages selected from the context menu for the solution.

  2. 按 [偵錯] 按鈕來啟動 Android 模擬器(或 [執行 > 開始偵錯]。

  3. 編輯 MainPage.xaml,並在 </StackLayout> 結尾前面新增此 XAML:

    <Button Text="Click Me" Clicked="Handle_Clicked" />
    
  4. 編輯 MainPage.xaml.cs,並將這段程式碼新增至類別結尾:

    int count = 0;
    void Handle_Clicked(object sender, System.EventArgs e)
    {
        count++;
        ((Button)sender).Text = $"You clicked {count} times.";
    }
    
  5. 偵錯 Android 上的應用程式:

    Screenshot shows the Android Emulator.

  6. 按一下滑鼠右鍵將 iOS 設定為 [啟始專案]

    Set the startup project to iOS

  7. 從下拉式清單中選取 iOS 模擬器,以偵錯 iOS 上的應用程式。

適用於 Mac 的逐步指示

Download Sample 下載範例

請遵循下列步驟及上面的影片進行:

  1. 選擇 [ 檔案 > 新方案... ] 或按 [ 新增專案...] 按鈕,然後選取 [多平臺 > 應用程式 > 空白窗體應用程式]:

    Blank Forms App

  2. 確定已選取 [Android] 和 [iOS],並搭配 [.NET Standard] 程式碼共用:

    Android and iOS, with .NET Standard

注意

您的應用程式名稱和組織標識碼只支援 A-Z、a-z、'_'、'.' 和數位。

  1. 以滑鼠右鍵按一下方案來還原 NuGet 套件:

    Screenshot shows Restore NuGet Packages selected from the context menu for the solution.

  2. 按 [偵錯] 按鈕來啟動 Android 模擬器(或 [執行 > 開始偵錯]。

  3. 編輯 MainPage.xaml,並在 </StackLayout> 結尾前面新增此 XAML:

    <Button Text="Click Me" Clicked="Handle_Clicked" />
    
  4. 編輯 MainPage.xaml.cs,並將這段程式碼新增至類別結尾:

    int count = 0;
    void Handle_Clicked(object sender, System.EventArgs e)
    {
        count++;
        ((Button)sender).Text = $"You clicked {count} times.";
    }
    
  5. 偵錯 Android 上的應用程式:

    Screenshot shows the Android Emulator.

  6. 按一下滑鼠右鍵將 iOS 設定為 [啟始專案]

    Set the startup project to iOS

  7. 偵錯 iOS 上的應用程式:

    iOS app

您可以從範例庫下載完整的程式碼,或在 GitHub 上進行檢視。

後續步驟