{"id":32172,"date":"2017-06-21T12:15:04","date_gmt":"2017-06-21T19:15:04","guid":{"rendered":"https:\/\/blog.xamarin.com\/?p=32172"},"modified":"2019-04-04T07:51:08","modified_gmt":"2019-04-04T14:51:08","slug":"building-xamarin-forms-apps-net-standard","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/xamarin\/building-xamarin-forms-apps-net-standard\/","title":{"rendered":"Building Xamarin.Forms Apps with .NET Standard"},"content":{"rendered":"<p>.NET Standard libraries bring our code to more platforms and give us even more APIs from the Base Class Libraries with which to build amazing apps. Developers have been able to <a href=\"https:\/\/blog.xamarin.com\/net-standard-library-support-for-xamarin\/\">use .NET Standard libraries with Xamarin<\/a> to share code across platforms, and use Xamarin.Forms to create a shared user interface across iOS, Android, UWP, macOS, for a long time. When combined, .NET Standard and Xamarin.Forms are a match made in heaven for maximizing the amount of code we can share across platforms, both in our underlying business logic as well as our user interface layer.<\/p>\n<p>In this blog post, you&#8217;ll learn how to combine .NET Standard libraries and Xamarin.Forms to maximize code sharing and productivity.<\/p>\n<h2>What is this .NET Standard thing?<\/h2>\n<p>.NET Standard is a <a href=\"https:\/\/docs.microsoft.com\/en-us\/dotnet\/standard\/net-standard\">formal specification of .NET APIs<\/a> that are intended to be available on all .NET runtimes (such as .NET Framework, Mono, and .NET Core). In real terms, you can think of this as a simplified, yet expanded, Portable Class Library. Any code added to a .NET Standard library can be used on any runtime that supports the .NET Standard Platform. In addition, we get expanded access to APIs within the .NET base class libraries, support more platforms, and we don&#8217;t ever have to deal with the madness that is PCL profiles.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/NetStandard.png\" width=\"487\" height=\"294\" alt=\".NET Standard Diagram\" class=\"aligncenter size-large\" \/><\/p>\n<h2>Bringing Xamarin.Forms to .NET Standard<\/h2>\n<p>The Xamarin.Forms 2.3.5-pre release added compatibility with .NET Standard. In addition to shared projects and Portable Class Libraries, you can now add .NET Standard libraries to your code sharing tool belt.<\/p>\n<h3>File -&gt; New<\/h3>\n<p>Let&#8217;s get started by creating a new Xamarin.Forms app. If you already have an existing Xamarin.Forms app you&#8217;re looking to migrate to a .NET Standard, you can skip directly to the next step.<\/p>\n<p>In Visual Studio, click <strong>File<\/strong> -&gt; <strong>New Project<\/strong> -&gt; <strong>Cross-Platform<\/strong> -&gt; <strong>Cross-Platform App<\/strong>. Select <strong>Master Detail<\/strong>, and click <strong>OK<\/strong>. This is going to create a scaffolded mobile app for iOS, Android, and UWP with Xamarin.Forms that displays some data in a list&mdash;a common mobile development recipe.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/1.Filenew.png\" alt=\"Shows a File -&gt; New Project for a mobile app in Visual Studio.\" width=\"716\" height=\"393\" class=\"aligncenter size-full wp-image-32177\" \/><\/p>\n<p>In Visual Studio for Mac, you can create a similar template by going to <strong>File<\/strong> -&gt; <strong>New<\/strong> -&gt; <strong>Multiplatform<\/strong> -&gt; <strong>Forms App<\/strong>.<\/p>\n<h3>Adding a .NET Standard Library<\/h3>\n<p>Adding a .NET Standard library to your app is super easy; it works just like adding any other library to your project!<\/p>\n<p>If you&#8217;re using Visual Studio 2017, right-click the solution and click <strong>Add<\/strong> -&gt; <strong>New Project<\/strong>. Navigate to the .NET Standard section and add a .NET Standard project.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/2.NETStandard.png\" alt=\"Add a .NET Standard library to your project.\" width=\"941\" height=\"443\" class=\"aligncenter size-full wp-image-32178\" \/><\/p>\n<p>Developers using Visual Studio for Mac can create a .NET Standard library by navigating to <strong>Multiplatform<\/strong> -&gt; <strong>Library<\/strong> -&gt; <strong>.NET Standard Library<\/strong>. If you&#8217;re using Visual Studio 2015, be sure to <a href=\"https:\/\/blog.xamarin.com\/net-standard-library-support-for-xamarin\/\">follow our getting started guide<\/a> to create and convert a Portable Class Library to a .NET Standard library.<\/p>\n<h3>Adding Xamarin.Forms to a .NET Standard Library<\/h3>\n<p>Just like Portable Class Libraries, we can add NuGets to .NET Standard libraries. Open the NuGet Package Manager by right-clicking the project and selecting <strong>Manage NuGet Packages<\/strong>. Switch to the <strong>Browse<\/strong> tab, select <strong>Include<\/strong> prerelease, and search for Xamarin.Forms. At the time of this blog post, only the prerelease version of Xamarin.Forms has compatibility with .NET Standard, although it is likely that .NET Standard compatibility will be included in the next stable release.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/3AddNuget.png\" alt=\"Add Xamarin.Forms NuGet to a .NET Standard library.\" width=\"958\" height=\"175\" class=\"aligncenter size-full wp-image-32180\" \/><\/p>\n<p>In each of the other projects (such as iOS, Android, UWP, etc.), upgrade to the latest prerelease Xamarin.Forms NuGet, add a reference to the .NET Standard library we created, and remove the reference to the shared project or PCL that contains your Xamarin.Forms UI logic.<\/p>\n<p>Finally, if you&#8217;re migrating, copy over files from your shared project or PCL to our .NET Standard library. That&#8217;s it! You&#8217;re now using a .NET Standard library with Xamarin.Forms to share code:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/NetStandard_001.png\" alt=\"Using .NET Standard to share code across all the platforms with Xamarin.Forms.\" width=\"1889\" height=\"793\" class=\"aligncenter size-full wp-image-32181\" \/><\/p>\n<h2>Video Walkthrough<\/h2>\n<p>Want to see it in action? Our very own James Montemagno walks through the upgrade path on <a href=\"http:\/\/xamarinshow.com\">The Xamarin Show<\/a> on Channel 9 with plenty of tips and tricks along the way:<\/p>\n<p><iframe src=\"https:\/\/channel9.msdn.com\/Shows\/XamarinShow\/Snack-Pack-15-Upgrading-to-XamarinForms-to-NET-Standard\/player\" width=\"640\" height=\"360\" allowFullScreen frameBorder=\"0\"><\/iframe><\/p>\n<h2>Wrapping Up<\/h2>\n<p>In this blog post, we created a brand new cross-platform mobile app and used a .NET Standard library as our code sharing strategy to reach all the platforms. To learn more about .NET Standard for Xamarin (without Xamarin.Forms), check out our blog on <a href=\"https:\/\/blog.xamarin.com\/net-standard-library-support-for-xamarin\/\">.NET Standard support<\/a>. If you&#8217;re still curious as to what .NET Standard is all about, I suggest you <a href=\"https:\/\/www.youtube.com\/watch?v=YI4MurjfMn8\">check out Immo Landwerth&#8217;s introductory video<\/a>, read up on the <a href=\"https:\/\/docs.microsoft.com\/en-us\/dotnet\/standard\/net-standard\">documentation on docs.microsoft.com<\/a>, or <a href=\"https:\/\/github.com\/dotnet\/standard\">follow along on GitHub<\/a>.\t\t<\/p>\n","protected":false},"excerpt":{"rendered":"<p>.NET Standard libraries bring our code to more platforms and give us even more APIs from the Base Class Libraries with which to build amazing apps. Developers have been able to use .NET Standard libraries with Xamarin to share code across platforms, and use Xamarin.Forms to create a shared user interface across iOS, Android, UWP, [&hellip;]<\/p>\n","protected":false},"author":546,"featured_media":39167,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[2],"tags":[16],"class_list":["post-32172","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-developers","tag-xamarin-forms"],"acf":[],"blog_post_summary":"<p>.NET Standard libraries bring our code to more platforms and give us even more APIs from the Base Class Libraries with which to build amazing apps. Developers have been able to use .NET Standard libraries with Xamarin to share code across platforms, and use Xamarin.Forms to create a shared user interface across iOS, Android, UWP, [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts\/32172","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\/546"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/comments?post=32172"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts\/32172\/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=32172"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/categories?post=32172"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/tags?post=32172"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}