建置您的第一個 應用程式

在本教學課程中,您將瞭解如何在 Windows 或 Visual Studio 2022 for Mac 上的 Visual Studio 2022 中建立和執行第一個 .NET 多平臺應用程式 UI (.NET MAUI) 應用程式。 這有助於確保您的開發環境已正確設定。

必要條件

  • 已安裝 .NET 多平臺應用程式 UI 工作負載的 Visual Studio 2022 17.8 或更高版本。 如需詳細資訊,請參閱 安裝

建立應用程式

在本教學課程中,您將在 Visual Studio 2022 中建立第一個 .NET MAUI 應用程式,並在 Android 模擬器上執行:

在 Windows 上開發適用於 iOS 的 .NET MAUI 應用程式需要 Mac 組建主機。 如果您不需要以 iOS 為目標,而且沒有 Mac,請考慮改為開始使用 Android 或 Windows。

在本教學課程中,您將在 Visual Studio 中建立第一個 .NET MAUI 應用程式,並在 iOS 模擬器上執行:

在本教學課程中,您將在 Visual Studio 2022 中建立第一個 .NET MAUI 應用程式,並在 Windows 上執行:

  1. 啟動 Visual Studio 2022。 在開始視窗中,按兩下 [建立新專案 ] 以建立新的專案:

    New solution.

  2. 在 [建立新專案] 視窗中,選取 [所有項目類型] 下拉式清單中的 [MAUI],選取 .NET MAUI 應用程式範本,然後按兩下 [下一步] 按鈕:

    Choose a template.

  3. 在 [ 設定新專案] 視窗中,為您的專案 命名,選擇適合它的位置,然後按兩下 [ 下一步 ] 按鈕:

    Configure the project.

  4. 在 [ 其他資訊] 視窗中,選擇您想要設為目標的 .NET 版本,然後按兩下 [ 建立] 按鈕:

    Additional information.

  5. 等候專案建立,以及還原其相依性:

    Restored dependencies.

  1. 在 Visual Studio 工具列中,使用 [偵錯目標] 下拉式清單選取 [Android 模擬器],然後選取 [Android 模擬器] 專案:

    Select the Android Emulator debugging target for .NET MAUI.

  2. 在 Visual Studio 工具列中 ,按 Android 模擬器 按鈕:

    Android emulator button.

    Visual Studio 會開始安裝預設的 Android SDK 和 Android 模擬器。

  3. 在 [ Android SDK - 許可協定] 視窗中,按 [ 接受 ] 按鈕:

    First Android SDK License Agreement window.

  4. 在 [ Android SDK - 許可協定] 視窗中,按 [ 接受 ] 按鈕:

    Second Android SDK License Agreement window.

  5. 在 [ 用戶帳戶控制 ] 對話框中,按 [ ] 按鈕:

    Android SDK license user account control dialog.

  6. 在 [授權接受] 視窗中,按 [接受] 按鈕:

    Android device license window.

    等候 Visual Studio 下載 Android SDK 和 Android Emulator。

  7. 在 Visual Studio 工具列中 ,按 Android 模擬器 按鈕:

    Android emulator button.

    Visual Studio 將會開始建立預設的 Android 模擬器。

  8. 在 [ 用戶帳戶控制 ] 對話框中,按 [ ] 按鈕:

    Android Device Manager user account control dialog.

  9. 在 [ 新增裝置 ] 視窗中,按 [ 建立] 按鈕:

    New Android Device window.

    等候 Visual Studio 下載、解壓縮及建立 Android 模擬器。

  10. 關閉 [Android 裝置管理員] 視窗:

    Android Device Manager window.

  11. 在 Visual Studio 工具列中,按 圖元 5 - API 34 (Android 14.0 - API 33) 按鈕來建置和執行應用程式:

    Pixel 5 API 34 emulator button.

    Visual Studio 會啟動 Android 模擬器、建置應用程式,並將應用程式部署至模擬器。

    警告

    必須啟用硬體加速,才能將Android模擬器效能最大化。 無法執行這項操作會導致模擬器執行速度非常慢。 如需詳細資訊,請參閱如何使用Android模擬器啟用硬體加速(Hyper-V和AEHD)。

  12. 在Android模擬器中執行的應用程式中,按下 [按下'按下'按鈕數次,並觀察按鈕點擊次數的計數遞增。

    App running in the Android emulator.

疑難排解

如果您的應用程式無法編譯,請檢閱 針對已知問題進行疑難解答,這可能會有問題的解決方案。 如果問題與 Android 模擬器有關,請參閱 Android 模擬器疑難解答

  1. 在 Visual Studio 工具列中,使用 [偵錯目標] 下拉式清單選取 [Framework],然後選取 net8.0-windows 專案:

    Select the Windows Machine debugging target for .NET MAUI.

  2. 在 Visual Studio 工具列中 ,按 [Windows 計算機 ] 按鈕來建置並執行應用程式:

    Run .NET MAUI app in Visual Studio button.

    如果您尚未啟用開發人員模式,Visual Studio 會提示您啟用它。 在 [啟用 Windows 開發人員模式] 對話框中,按兩下開發人員的設定以開啟 設定 應用程式:

    Enable Windows developer mode dialog.

    在 設定 應用程式中,開啟開發人員模式並接受免責聲明:

    Developer Mode toggle on the Windows 11 settings app.

    關閉 設定 應用程式,然後關閉 [啟用 Windows 開發人員模式] 對話方塊。

  3. 在執行中的應用程式中,按下 [按下我 ] 按鈕數次,並觀察按鈕點選次數的計數遞增:

    .NET MAUI app running on Windows.

疑難排解

如果您的應用程式無法編譯,請檢閱 針對已知問題進行疑難解答,這可能會有問題的解決方案。

  1. 在 Visual Studio 中,將 IDE 與 Mac 組建主機配對。 如需詳細資訊,請參閱 與Mac配對以進行iOS開發

  2. 在 Visual Studio 工具列中,使用 [ 偵錯目標 ] 下拉式清單選取 [iOS 模擬器],然後選取 特定的 iOS 模擬 器:

    Visual Studio iOS simulators debug targets.

  3. 在 Visual Studio 工具列中,按所選 iOS 模擬器的 [開始] 按鈕,以建置並執行您的應用程式:

    Visual Studio iOS simulator debug target choice.

    Visual Studio 會建置應用程式、啟動適用於 Windows 的遠端 iOS 模擬器,並將應用程式部署至遠端模擬器。 如需 Windows 遠端 iOS 模擬器的詳細資訊,請參閱 適用於 Windows 的遠端 iOS 模擬器。

  4. 在執行中的應用程式中,按下 [按下我 ] 按鈕數次,並觀察按鈕點擊次數的計數遞增。

    .NET MAUI app running in iOS Simulator on a Mac.

疑難排解

如果您的應用程式無法編譯,請檢閱 針對已知問題進行疑難解答,這可能會有問題的解決方案。

以 Mac Catalyst 為目標的 .NET MAUI 應用程式只能使用 Visual Studio 2022 for Mac 來啟動和偵錯。

下一步

在本教學課程中,您已瞭解如何建立並執行您的第一個 .NET 多平台應用程式 UI (.NET MAUI) 應用程式。

若要瞭解使用 .NET MAUI 建置應用程式的基本概念,請參閱 建立 .NET MAUI 應用程式。 或者,如需完整的 .NET MAUI 訓練課程,請參閱 使用 .NET MAUI 建置行動和桌面應用程式。