Xamarin 熱重新開機Xamarin Hot Restart

Xamarin 熱重新開機可讓您在開發期間快速測試應用程式的變更,包括多檔案程式碼編輯、資源和參考。Xamarin Hot Restart enables you to quickly test changes to your app during development, including multi-file code edits, resources, and references. 它會將新變更推送至在偵錯目標上的現有應用程式套件組合,如此可達到更快速的組建和部署循環。It pushes the new changes to the existing app bundle on the debug target which results in a much faster build and deploy cycle.

重要

Xamarin 熱重新開機目前已在 Visual Studio 2019 版本16.5 穩定推出,並支援使用的 iOS 應用程式 Xamarin.Forms 。Xamarin Hot Restart is currently available in Visual Studio 2019 version 16.5 stable and supports iOS apps using Xamarin.Forms. 藍圖中有 Visual Studio for Mac 和非 Xamarin.Forms 應用程式的支援。Support for Visual Studio for Mac and non-Xamarin.Forms apps is on the roadmap.

規格需求Requirements

  • Visual Studio 2019 16.5 版或更高版本Visual Studio 2019 version 16.5 or higher
  • iTunes (Microsoft Store 或64位版本) iTunes (Microsoft Store or 64-bit versions)
  • Apple 開發人員帳戶和付費 Apple 開發人員計畫 註冊Apple Developer account and paid Apple Developer Program enrollment

初始設定Initial setup

注意

Visual Studio 16.8 和舊版上預設會停用 Xamarin 熱重新開機。Xamarin Hot Restart is disabled by default on Visual Studio 16.8 and previous versions. 您可以在 [工具] > 選項] 下啟用此 功能 > 環境 > 預覽功能,> 啟用 Xamarin 熱重新開機You can enable it under Tools > Options > Environment > Preview Features > Enable Xamarin Hot Restart. 從 Visual Studio 16.9 開始,依預設會開啟 Xamarin 熱重新開機,而且可以從 工具 > 選項 > Xamarin > IOS 設定 > 啟用熱重新開機Starting in Visual Studio 16.9, Xamarin Hot Restart is on by default and can be turned off from Tools > Options > Xamarin > iOS Settings > Enable Hot Restart.

  1. 確定已將 iOS 專案設定為啟始專案,並將組建設定設為 Debug | iPhoneEnsure the iOS project is set as the startup project and the build configuration is set to Debug|iPhone.

    1. 如果這是現有的專案,請移至 [建置] > [組態管理員...]If this is an existing project, go to Build > Configuration Manager… 並確定已針對 iOS 專案啟用 [部署]。and ensure Deploy is enabled for the iOS project.
  2. 選取並按一下工具列中的 [本機裝置] 以啟動安裝精靈:Select and click Local Device in the toolbar to launch the setup wizard:

    Visual Studio 工具列的螢幕擷取畫面,其中已將本機裝置設定為偵錯目標。Screenshot of the Visual Studio toolbar with local device set as the debug target.

  3. 如果未安裝 iTunes,請按一下 [下載 iTunes] 來下載安裝程式。If iTunes is not installed, click Download iTunes to download the installer. iTunes 安裝完成後,請按一下 [下一步]。Click Next when the iTunes installation is complete.

  4. 將 iOS 裝置連接到您的電腦。Connect an iOS device to your machine. 如果裝置已經插入,請將它中斷連接。If a device was already plugged in, unplug then reconnect it. 裝置名稱會在偵測到時出現在精靈中。The device name will appear in the wizard once it is detected. 按一下 [下一步] 。Click Next.

  5. 輸入您的 Apple 開發人員帳戶認證,然後按一下 [下一步]。Enter your Apple Developer account credentials and click Next.

  6. 使用下拉式功能表選取開發小組,以便在專案中啟用自動化佈建Select a development team using the dropdown menu in order to enable automatic provisioning in the project. 按一下 [完成] 。Click Finish.

注意

建議使用自動化佈建,這樣便可以輕鬆地針對部署設定額外的 iOS 裝置。Using automatic provisioning is recommended so additional iOS devices can be easily configured for deployment. 不過,如果有正確的佈建設定檔,則您可以將其停用並繼續使用手動佈建。However, you can disable it and continue using manual provisioning if the correct provisioning profiles are present.

使用 Xamarin 熱重新開機Use Xamarin Hot Restart

初始設定之後,連線的裝置會出現在偵錯目標下拉式功能表中。After the initial setup, your connected device will appear in the debug target dropdown menu. 若要針對應用程式進行偵錯,請在下拉式清單中選取裝置,然後按一下 [執行] 按鈕。To debug your app, select your device in the dropdown and click the Run button. 您可能會在 Visual Studio 中看到一則訊息,要求您在裝置上手動啟動應用程式,以便啟動偵錯工作階段。You may see a message in Visual Studio asking you to manually launch the app on the device in order to start the debug session.

您可以在偵錯時對程式碼檔案進行編輯,然後按下偵錯工具列中的 [重新啟動] 按鈕,或使用 Ctrl+Shift+F5 來重新啟動偵錯工作階段,並套用新變更:You can make edits to your code files while debugging, then press the Restart button in the debug toolbar or use Ctrl+Shift+F5 to restart the debug session with your new changes applied:

偵錯工具列的螢幕擷取畫面,其中已顯目提示重新開機按鈕。Screenshot of the debug toolbar with the restart button highlighted.

您也可以使用 HOTRESTART 預處理器符號來防止在使用 Xamarin 熱重新開機進行偵錯工具時執行特定程式碼。You can also use the HOTRESTART preprocessor symbol to prevent certain code from executing when debugging with Xamarin Hot Restart.

限制Limitations

  • Xamarin.Forms目前僅支援使用和 ios 裝置建立的 ios 應用程式。Only iOS apps built with Xamarin.Forms and iOS devices are currently supported.
  • 僅支援64位的 iOS 裝置。Only 64-bit iOS devices are supported. 從 iOS 11 起,Apple 不再允許在32位架構上執行 iOS 應用程式, (iPhone 5 秒) 之前的裝置。As of iOS 11, Apple no longer allows running iOS apps on the 32-bit architecture (devices earlier than iPhone 5s).
  • 不支援腳本和 XIB 檔案,如果應用程式嘗試在執行時載入這些,便可能損毀。Storyboard and XIB files are not supported and the app may crash if it attempts to load these at runtime. 使用 HOTRESTART 預處理器符號來防止此程式碼執行。Use the HOTRESTART preprocessor symbol to prevent this code from executing.
  • 靜態 iOS 程式庫和架構不受支援,如果您的應用程式嘗試載入這些錯誤,您可能會看到執行階段錯誤或損毀。Static iOS libraries and frameworks are not supported and you may see runtime errors or crashes if your app attempts to load these. 使用 HOTRESTART 預處理器符號來防止此程式碼執行。Use the HOTRESTART preprocessor symbol to prevent this code from executing. 支援動態 iOS 程式庫。Dynamic iOS libraries are supported.
  • 您無法使用 Xamarin 熱重新開機來建立用於發佈的應用程式套件組合。You cannot use Xamarin Hot Restart to create app bundles for publishing. 您仍然需要 Mac 電腦,才能為應用程式進行完整編譯、簽署和部署到生產環境。You will still need a Mac machine to do a full compilation, signing, and deployment for your application to production.
  • 目前不支援資產目錄。Asset Catalogs are currently not supported. 使用熱重新開機時,您的應用程式會顯示 Xamarin 應用程式的預設圖示和啟動畫面。When using Hot Restart, your app will show the default icon and launch screen for Xamarin apps. 當與 Mac 配對,或在 Mac 上進行開發時,您的資產目錄將會運作。When paired to a Mac, or developing on a Mac, your Asset Catalogs will work.

疑難排解Troubleshoot

  • 有一個已知問題,那就是啟用裝置專屬組建會導致應用程式無法進入偵錯模式。There is a known issue where having device-specific builds enabled prevents the app from entering debug mode. 因應措施是在 [屬性] > [iOS 組建] 下停用此功能,然後重試偵錯。Workaround is to disable this under Properties > iOS Build and retry debugging. 未來的版本中將會修正此問題。This will be fixed in a future release.
  • 如果應用程式已存在於裝置上,則嘗試使用熱重新開機進行部署可能會失敗,並出現 AMDeviceStartHouseArrestService 錯誤。If the app is already present on the device, trying to deploy with Hot Restart may fail with a AMDeviceStartHouseArrestService error. 因應措施是在裝置上解除安裝應用程式,然後再次部署。The workaround is to uninstall the app on the device then deploy again.
  • 輸入不屬於 Apple 開發人員計畫的 Apple ID,可能會導致下列錯誤: Authentication Error. Xcode 7.3 or later is required to continue developing with your Apple IDEntering an Apple ID that is not part of the Apple Developer Program might result in the following error: Authentication Error. Xcode 7.3 or later is required to continue developing with your Apple ID. 您必須擁有有效的 Apple 開發人員帳戶,才能在 iOS 裝置上使用 Xamarin 熱重新開機。You must have a valid Apple Developer account to use Xamarin Hot Restart on iOS devices.

若要回報其他問題,請使用意見反應工具,工具位於 [說明] > [傳送意見反應] > [回報問題]To report additional issues, please use the feedback tool at Help > Send Feedback > Report a Problem.