.NET Standard is a formal specification of the .NET APIs that allow you to share even more code across the platforms you target. Xamarin developers are able to use .NET Standard libraries for sharing business logic, and .NET Standard support came to Xamarin.Forms recently, allowing you to easily share both your business and UI logic across multiple platforms. You’ve been asking us when you can File -> New -> Mobile App with Xamarin.Forms and .NET Standard, and we’re excited to share that this is now available in the Visual Studio 2017 version 15.5 Preview 3.
In this blog post, you’ll learn how to use new project templates in Visual Studio 2017 that enable you to utilize .NET Standard as a code sharing strategy when creating new Xamarin.Forms apps. We’ll also take a look at some of the other improvements that landed in our Xamarin.Forms templates in Visual Studio 2017 version 15.5, such as migration to PackageReference and checkboxes for selecting which platforms to target.
File -> New Project
You can get started with a new Xamarin.Forms app by going to File -> New -> Visual C# -> Cross-Platform -> Cross-Platform App. This template allows you to select what kind of starter project you’d like, what platforms you plan on targeting, and your code sharing strategy.
.NET Standard is a formal specification of .NET APIs 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 (.NET Standard 2.0 adds over 20,000 new APIs!), support more platforms, and we don’t ever have to deal with the madness that is PCL profiles.
To use the .NET Standard code sharing strategy with Xamarin.Forms, select the .NET Standard radio button. Portable Class Library is no longer available as an option from File -> New, but existing class library projects will continue to build and work!
.NET developers know the pain that is
packages.config. This file is meant to be a canonical list of all NuGet packages referenced by an app, but quickly turns into an unmanageable mess. The problem with
packages.config is that it’s not just a list of all the packages explicitly added, but also the packages my packages reference.
PackageReference is a new way to manage your NuGets directly in the .csproj file, rather than a separate file. One of my favorite things about PackageReference is that all packages listed are transitively restored, meaning that you only need to define the top-level NuGets that your app consumes; no more dealing with the 100s of packages that your packages reference.
To manage packages, you can use the NuGet Package Manager or directly edit your .csproj to add something like this:
<PackageReference Include="Xamarin.Forms" Version="220.127.116.112" />
All templates in our cross-platform app wizard now use
PackageReference by default.
Pick Your Platform
Mobile developers often begin a project with certain platforms in mind. We wanted our File -> New Project to reflect that experience, so we added checkboxes to allow you to select which platforms you care about when creating mobile apps.
This blog post talked about how you can File -> New -> Mobile App with Xamarin.Forms and .NET Standard. We also covered some key improvements to our cross-platform templating wizard, including migration to
PackageReference and target platform checkboxes for selecting which platforms you care about.
You can get these changes today by downloading Visual Studio 2017 version 15.5, currently in preview. With Visual Studio 2017, it’s now easier and safer for you to try new Xamarin previews. Visual Studio 2017 Preview installs side-by-side with Visual Studio 2017, and is sandboxed to prevent anything in your preview environment from affecting your stable environment, so be sure to give our new templates a try today! These templates are Visual Studio-only for the moment, but will be making their way to Visual Studio for Mac sometime soon.