{"id":46121,"date":"2020-01-10T10:04:20","date_gmt":"2020-01-10T18:04:20","guid":{"rendered":"http:\/\/devblogs.microsoft.com\/xamarin\/?p=46121"},"modified":"2022-04-26T09:20:40","modified_gmt":"2022-04-26T16:20:40","slug":"tips-tricks-xaml-hot-reload","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/xamarin\/tips-tricks-xaml-hot-reload\/","title":{"rendered":"Tips and Tricks for XAML Hot Reload"},"content":{"rendered":"<p>Happy 2020, Xamarin community! To kick start your productive new year, we want to share some favorite tips for <a href=\"https:\/\/devblogs.microsoft.com\/xamarin\/public-preview-xaml-hot-reload-xamarin-forms\/\">XAML Hot Reload for Xamarin.Forms<\/a>. With XAML Hot Reload, you can make changes to your XAML files, hit save, and immediately see those changes reflected in your running app!<\/p>\n<h2>Tips and Tricks for XAML Hot Reload \u00a0<\/h2>\n<h3><strong>Getting Set Up<\/strong> XAML Hot Reload is built-in to Visual Studio 16.3 and Visual Studio for Mac 8.3. To turn it on, go to Tools > Options > Xamarin > Hot Reload, or Visual Studio > Preferences > Projects > Xamarin Hot Reload. In the latest releases, 16.4 and 8.4, XAML Hot Reload is turned on for you by default! For new projects, the templates are set up to start using XAML Hot Reload immediately. If you\u2019re working on an older project, make sure you have at least Xamarin.Forms 4.1. Then, follow this flow chart to double check your build settings. For more details, you can look at the<\/h3>\n<p><a href=\"https:\/\/www.aka.ms\/xamlhotreloaddocs\">XAML Hot Reload Documentation<\/a>. <img decoding=\"async\" class=\"size-medium wp-image-46123 aligncenter\" src=\"http:\/\/devblogs.microsoft.com\/xamarin\/wp-content\/uploads\/sites\/44\/2020\/01\/hotreloadflowchart-e1578428145268-300x272.png\" alt=\"XAML Hot Reload Tips and Tricks\" width=\"300\" height=\"272\" srcset=\"https:\/\/devblogs.microsoft.com\/xamarin\/wp-content\/uploads\/sites\/44\/2020\/01\/hotreloadflowchart-e1578428145268-300x272.png 300w, https:\/\/devblogs.microsoft.com\/xamarin\/wp-content\/uploads\/sites\/44\/2020\/01\/hotreloadflowchart-e1578428145268-768x697.png 768w, https:\/\/devblogs.microsoft.com\/xamarin\/wp-content\/uploads\/sites\/44\/2020\/01\/hotreloadflowchart-e1578428145268.png 793w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/p>\n<h3><strong>Making the Most of MVVM<\/strong> XAML Hot Reload works by reloading the entire XAML view you edited and its children elements. In a future release, we want it to just reload what you changed, but for now a good rule of thumb is to set your Binding Context in your code behind. If you set the Binding Context in C#, it won\u2019t get recreated every time you make a change to your XAML and save. However, in some cases, XAML needs that Binding Context for IntelliSense related to your bindings. If you set it only in the code behind, you\u2019d lose that IntelliSense. If you set it in XAML and code behind, you create that Binding Context twice, which doesn\u2019t work! The best way to handle this is with<\/h3>\n<p><a href=\"https:\/\/docs.microsoft.com\/en-us\/xamarin\/xamarin-forms\/xaml\/xaml-previewer\/design-time-data\">Design Time Data<\/a>. With that, you set the Binding Context in your code behind, and use the \u201cd:\u201d prefix (meaning, use at design time only) in XAML. This way, it\u2019s only set it up once at runtime, so XAML Hot Reload won\u2019t refresh your Binding Context and you\u2019ll still get IntelliSense as you edit your XAML bindings. This is what that looks like in code: Code behind:<\/p>\n<pre class=\"\">public DetailsPage(Monkey monkey)\n{\n \u00a0\u00a0 InitializeComponent();\n \u00a0\u00a0 BindingContext = new DetailsViewModel(monkey);\n}<\/pre>\n<p>\u00a0 Add to XAML Page headers:<\/p>\n<pre class=\"lang:default decode:true\">xmlns:d=\"http:\/\/xamarin.com\/schemas\/2014\/forms\/design\"\nxmlns:mc=\"http:\/\/schemas.openxmlformats.org\/markup-compatibility\/2006\"\nmc:Ignorable=\"d\"\nxmlns:vm=\"clr-namespace:Monkeys.ViewModels\"<\/pre>\n<p>\u00a0 XAML:<\/p>\n<pre class=\"\">&lt;d:ContentPage.BindingContext&gt;\n \u00a0\u00a0 &lt;vm:DetailsViewModel \/&gt;\n&lt;\/d:ContentPage.BindingContext&gt;<\/pre>\n<p>Also, XAML Hot Reload currently calls the OnAppearing() method with every refresh, so be mindful of what you load there.<\/p>\n<h3><strong>Yes, You Can Reload That!<\/strong> XAML Hot Reload isn\u2019t limited to just your views and controls. You can also reload resources in App.xaml or in global resource dictionaries. Additionally, you can reload changes to your AppShell.xaml, which is where you set up the structure of a<\/h3>\n<p><a href=\"https:\/\/docs.microsoft.com\/en-us\/xamarin\/xamarin-forms\/app-fundamentals\/shell\/\">Xamarin.Forms Shell application<\/a>. In the latest releases of Visual Studio and Visual Studio for Mac, you can even reload CSS changes both inline in your XAML and in a separate CSS file in your Xamarin.Forms project.<\/p>\n<h3><strong>What\u2019s Next?<\/strong> We have a lot of new features coming in 2020! If you have a suggestion or find a bug, please send them to us using Help > Send Feedback > Report a Problem (or Suggest a Feature). Some popular suggestions that you can vote on are:<\/h3>\n<ul>\n<li><a href=\"https:\/\/developercommunity.visualstudio.com\/idea\/661682\/xaml-hot-reload-for-xamarinforms-on-uwp.html\">XAML Hot Reload for Xamarin.Forms UWP<\/a><\/li>\n<li><a href=\"https:\/\/developercommunity.visualstudio.com\/idea\/650684\/c-hot-reload-xamarin.html\">C# Hot Reload for Xamarin.Forms<\/a><\/li>\n<li><a href=\"https:\/\/developercommunity.visualstudio.com\/idea\/699571\/allow-hotreload-on-multiple-devicesemulators-at-th.html\">Allow Hot Reload on multiple devices\/emulators at the same time<\/a> As always, feel free to reach out with questions and feedback at any time. Happy New Year!<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Kick start your productive new year with some tips and tricks for XAML Hot Reload for Xamarin.Forms. Such as making changes to your XAML files, saving, and instantly seeing those changes reflected on your app!<\/p>\n","protected":false},"author":1965,"featured_media":39167,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[2,1,291,367,7745],"tags":[7746,5841,6604],"class_list":["post-46121","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-developers","category-xamarin","category-xamarin-platform","category-xamarin-forms","category-xaml","tag-tips-tricks","tag-xaml","tag-xaml-hot-reload"],"acf":[],"blog_post_summary":"<p>Kick start your productive new year with some tips and tricks for XAML Hot Reload for Xamarin.Forms. Such as making changes to your XAML files, saving, and instantly seeing those changes reflected on your app!<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts\/46121","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/users\/1965"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/comments?post=46121"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts\/46121\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/media\/39167"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/media?parent=46121"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/categories?post=46121"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/tags?post=46121"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}