的 XAML 热重载 Xamarin.FormsXAML Hot Reload for Xamarin.Forms
03/14/2020
本文内容
XAML 热重载将插入现有的工作流,以提高工作效率并节省时间。XAML Hot Reload plugs into your existing workflow to increase your productivity and save you time. 如果不使用 XAML 热重载,则必须在每次想要查看 XAML 更改时生成并部署应用。Without XAML Hot Reload, you have to build and deploy your app every time you want to see a XAML change. 如果是热重载,则保存 XAML 文件时,所做的更改将反映在正在运行的应用程序中。With Hot Reload, when you save your XAML file the changes are reflected live in your running app. 此外,还将保留你的导航状态和数据,从而使你可以快速循环访问 UI,而不会丢失你在应用中的位置。In addition, your navigation state and data will be maintained, enabling you to quickly iterate on your UI without losing your place in the app. 因此,通过 XAML 热重载,你将花费更少的时间来重新生成和部署应用,以验证 UI 更改。Therefore, with XAML Hot Reload, you'll spend less time rebuilding and deploying your apps to validate UI changes.
备注
If you're writing a WPF or UWP app, see XAML Hot Reload for UWP and WPF.
的 XAML 热重载 Xamarin.Forms 当前 不适 用于 Xamarin.Forms UWP 项目。XAML Hot Reload for Xamarin.Forms does not currently work for Xamarin.Forms UWP projects.
系统要求System requirements
IDE/框架IDE/Framework
所需版本Version Required
Visual Studio 2019Visual Studio 2019
16.4 或更高版本16.4 or greater
Visual Studio 2019 for MacVisual Studio 2019 for Mac
8.4 或更高版本8.4 or greater
Xamarin.Forms
4.1 或更高版本4.1 or greater
为启用 XAML 热重载 Xamarin.FormsEnable XAML Hot Reload for Xamarin.Forms
如果从模板开始,则默认情况下将启用 XAML 热重载,并将项目配置为不使用其他设置。If you are starting from a template, XAML Hot Reload is on by default and the project is configured to work with no additional setup. 在 Android 或 iOS 模拟器、模拟器或物理设备上调试应用程序,更改 XAML,并保存文件以触发 XAML 热重载。Debug your app on an Android or iOS emulator, simulator, or physical device, change your XAML, and save your file to trigger a XAML Hot Reload.
如果你正在使用现有 Xamarin.Forms 解决方案,则无需进行其他安装即可使用 XAML 热重载,但你可能需要仔细检查配置,以确保获得最佳体验。If you're working from an existing Xamarin.Forms solution, no additional installation is required to use XAML Hot Reload, but you might have to double check your configuration to ensure the best experience. 首先,在 IDE 设置中启用它:First, enable it in your IDE settings:
在 Windows 上,选中 "在工具选项中启用 xamarin 热重载" > 选项" > Xamarin > 热重载"。On Windows, check the Enable Xamarin Hot Reload checkbox at Tools > Options > Xamarin > Hot Reload.
在 Mac 上,选中 "在Visual Studio中启用 xamarin 热重载" > Preferences > 工具以进行 xamarin > XAML 热重载。On Mac, check the Enable Xamarin Hot Reload checkbox at Visual Studio > Preferences > Tools for Xamarin > XAML Hot Reload.
在早期版本的 Visual Studio for Mac 中,菜单位于Visual Studio > 首选项 > 项目 > Xamarin 热重载。In earlier versions of Visual Studio for Mac, the menu is at Visual Studio > Preferences > Projects > Xamarin Hot Reload.
然后,在 Android 和 iOS 生成设置中,检查链接器是否已设置为 "不链接" 或 "链接无"。Then, in your Android and iOS build settings, check that the Linker is set to "Don't Link" or "Link None". 若要在物理 iOS 设备上使用 XAML 热重载,还必须选中 "启用 Mono 解释 器 (visual studio 16.4 及更高版本) 或将 解释 器添加到 (visual Studio 16.3) 和更高版本的 其他 mtouch 参数 。To use XAML Hot Reload with a physical iOS device, you also have to check Enable the Mono interpreter (Visual Studio 16.4 and above) or add --interpreter to your Additional mtouch args (Visual Studio 16.3 and below).
您可以使用以下流程图检查您的现有项目的设置,以便与 XAML 热重载一起使用:You can use the following flowchart to check your existing project's setup for use with XAML Hot Reload:
复原重装Resilient reloading
如果进行了更改,则无法重新加载 XAML 热重载,它将使用 IntelliSense 显示错误消息。If you make a change that XAML Hot Reload can't reload, it will show you an error message using IntelliSense. 这些更改称为 "强制编辑",其中包括在 XAML 中键入的内容,或者将控件布线到不存在的事件处理程序。These changes, known as rude edits, include mistyping your XAML or wiring a control to an event handler that doesn't exist. 即使使用强制编辑,也可以继续重新加载,而无需重新启动应用程序-在 XAML 文件中的其他位置进行另一个更改,然后点击 "保存"。Even with a rude edit, you can continue to reload without restarting the app - make another change elsewhere in the XAML file and hit save. 不会重新加载 "强制编辑",但会继续应用其他更改。The rude edit won't be reloaded, but your other changes will continue to be applied.
一次在多个平台上重新加载Reload on multiple platforms at once
XAML 热重载支持在 Visual Studio 中同时进行调试,并 Visual Studio for Mac。XAML Hot Reload supports simultaneous debugging in Visual Studio and Visual Studio for Mac. 你可以同时部署 Android 和 iOS 目标,以查看在两个平台上同时反映的更改。You can deploy an Android and an iOS target at the same time to see your changes reflected on both platforms at once. 若要在多个平台上进行调试,请参阅:To debug on multiple platforms, see:
已知的限制Known limitations
Xamarin.Forms尚不支持其他目标(如 UWP 和 macOS)。Other Xamarin.Forms targets, such as UWP and macOS, are not yet supported. 可在 此处跟踪 UWP 支持的进度。You can track the progress of UWP support here.
在 XAML 热重载会话期间,无法添加、删除或重命名文件或 NuGet 包。You can't add, remove, or rename files or NuGet packages during a XAML Hot Reload session. 如果添加或删除文件或 NuGet 包,请重新生成并重新部署应用,以便继续使用 XAML 热重载。If you add or remove a file or NuGet package, rebuild and redeploy your app to continue using XAML Hot Reload.
将链接器设置为 "无" 链接 或 链接 ,以获得最佳体验。Set your linker to Don't Link or Link None for the best experience. 仅限链接 SDK设置工作正常,但在某些情况下可能会失败。The Link SDK only setting works most of the time, but it may fail in certain cases. 可在 Android 和 iOS 生成选项中找到链接器设置。Linker settings can be found in your Android and iOS build options.
在物理 iPhone 上调试需要解释器使用 XAML 热重载。Debugging on a physical iPhone requires the interpreter to use XAML Hot Reload. 为此,请打开项目设置,选择 "iOS 生成" 选项卡,并确保启用 "Mono 解释器" 设置。To do this, open the project settings, select the iOS Build tab, and ensure Enable the Mono interpreter setting is enabled. 可能需要将属性页顶部的 " 平台 " 选项更改为 " iPhone"。You may need to change the Platform option at the top of the property page to iPhone.
通过使用控件的值将控件分配给另一个字段或属性而创建的任何引用 x:Name 都不会重新加载。Any references created by assigning a control to another field or property using its x:Name value won't be reloaded.
在 AppShell 中更新 Shell 应用程序的可视层次结构可能会导致应用程序的状态保持有问题。Updating the visual hierarchy of your Shell application in AppShell.xaml can cause issues maintaining the state of your application. 如果遇到问题,请重新生成应用程序以继续重新加载。If you experience issues, rebuild the app to continue reloading.
XAML 热重载无法重新加载 c # 代码,包括事件处理程序、自定义控件、页代码隐藏以及其他类。XAML Hot Reload can't reload C# code, including event handlers, custom controls, page code-behind, and additional classes.
更多资源More resources
疑难解答Troubleshooting
如果 XAML 热重载未能初始化:If XAML Hot Reload fails to initialize:
更新 Xamarin.Forms 版本。Update your Xamarin.Forms version.
确保你处于最新版本的 IDE。Ensure you are on the latest version of the IDE.
将 Android 或 iOS 链接器设置设置为 " 不链接 项目的生成设置"。Set your Android or iOS Linker settings to Don't Link in the project's build settings.
如果保存 XAML 文件后没有任何反应,请确保在 IDE 中启用了 XAML 热重载。If nothing happens upon saving your XAML file, ensure that XAML Hot Reload is enabled in the IDE.
如果是在物理 iPhone 上进行调试,而应用程序无响应,请检查是否已启用解释器。If you're debugging on a physical iPhone and your app becomes unresponsive, check that the interpreter is enabled. 若要启用它,请选中 "启用 Mono 解释器 (visual studio 16.4/8.4,并向上) 或添加 --解释器 (visual studio) 16.3 Additional mtouch argumentsTo turn it on, check Enable the Mono interpreter (Visual Studio 16.4/8.4 and up) or add --interpreter to the Additional mtouch arguments field (Visual Studio 16.3/8.3 and prior) in your iOS Build settings.
若要报告 bug,请使用帮助 > 发送反馈 > 报告 Windows 上的问题,并帮助 > 报告 Mac 上的问题。To report a bug, use Help > Send Feedback > Report a Problem on Windows, and Help > Report a Problem on Mac.



