Create Cross Platform Mobile App with .NETStandard2.0Marvin HengTwitter : @hmhengBlog : http://hmheng.azurewebsites.netGithub: https://github.com/hmheng
Pre-requisite• Installed Visual Studio 2017 for Windows with Xamarin
Cross-Platform component & all necessary platform-specific SDK.
• Xamarin.Forms 2.4 or above. (Click here to learn how to create a Xamarin.Forms app with Portable Class Library)
Introduction to Visual Studio 2017• The First Class fully-featured integrated development
environment (IDE) for Android, iOS, Windows, web, and cloud development.
• Community free version comes with IntelliSense & connects to team management tools, such as Microsoft Team Services or GitHub, that increases developer’s efficiency & improves the experience.
• Available on Windows & MacOS.
Introduction to Visual Studio 2017Download & Install VS2017
on your PC or Mac if you don’t have one yet.
CLICK TO DOWNLOAD COMMUNITY VERSION for Free
Why Would We Need .NETStandard?• Defines uniform set of BCL APIs for all .NET
implementations to implement, independent of workload.• Enables developers to produce portable libraries that are
usable across .NET implementations, using this same set of APIs.
• Reduces or even eliminates conditional compilation of shared source due to .NET APIs, only for OS APIs.
Create MobileApp with .NETStandard 2.01. After you have installed Visual Studio 2017 (VS2017) with Cross-Platform development, launch it.
Create MobileApp with .NETStandard 2.02. We will create a Cross-Platform mobile app for Android, iOS& Windows UWP app. Let’s Select File -> New -> Project….
2
If you already have a Xamarin.Forms app, click here to jump over how to migrate your Existing Xamarin.Forms app from PCL to .NETStandard
Create MobileApp with .NETStandard 2.03. Select Cross-Platform -> Cross Platform App (Xamarin) -> Enter your App Name and then Hit OK.
3a
3b
3c
3d
Create MobileApp with .NETStandard 2.04. To make the following steps simple, we shall select Blank App with Xamarin.Forms & PCL now. Then click OK.
4b
4a
4c
Create MobileApp with .NETStandard 2.05. While creating the platform-specified projects, VS2017 will ask you for a Minimum Target Version & select Build 16299.
5
Create MobileApp with .NETStandard 2.06. Now, your app is ready to be compiled & running on your PC/Android. But, we want to develop with .NETStandard 2.0!
2a
Create MobileApp with .NETStandard 2.07. So, in order to migrate it from PCL to .NETStandard 2.0, we need to right click Solution at Solution Explorer.
2a
7
Create MobileApp with .NETStandard 2.08. Now, we will add a new .NETStandard 2.0 project to replace PCL. Right click the Solution, Select Add -> New Project….
2a
8a
8b
Create MobileApp with .NETStandard 2.09. Select .NET Standard -> Class Library -> Enter <namespace>.Core. Then click OK.
2a
9a
9b
9c9e
Create MobileApp with .NETStandard 2.010. Pull all necessary files from PCL to .NET Standard project.
10a
10b
Create MobileApp with .NETStandard 2.011. We can delete the Class1.cs since we do not need it.
11a
11b
Create MobileApp with .NETStandard 2.012. Now, we can delete PCL. Right click the PCL in Solution Explorer. Remove it from solution.
12a
12b
Create MobileApp with .NETStandard 2.013. After deleting PCL project, we need to install Xamarin.forms & .NETStandard library from NuGet.
13a
13b
Create MobileApp with .NETStandard 2.014. First, let’s search for & install .NETStandard library to all other projects.
14a
14b
14c
Create MobileApp with .NETStandard 2.015. To add Xamarin Forms to our newly created .NETStandardproject, search for Xamarin.Forms & install to the project.
15a
15b
15c
Publish First Web Application to Azure16. You may get error of duplication while trying to build. We need to remove following line from the project’s .csproj file.
16a
16b
Publish First Web Application to Azure17. We add reference to .NETStandard project to all other platform specific. Right click Project -> Add -> Reference
17a
17b
17c
Create MobileApp with .NETStandard 2.018. Tick the newly created project and click OK. Repeat the same for Android & iOS.
18a
18b
Create MobileApp with .NETStandard 2.019. Now, let’s connect our phone & re-deploy again!
19
Congratulation!Your Cross Platform Mobile App is now with .NET Standard 2.0!
ExtrasIn case you need to install some PCL libraries to your new .NETStandard’s Xamarin Forms, you will need to add this to <namespace>.Core.csproject.
a
b
c
<PropertyGroup><TargetFramework>netstandard2.0</TargetFramework><PackageTargetFallback>$(PackageTargetFallback);portable-win+net45+wp8+win81+wpa8</PackageTargetFallback>
</PropertyGroup>
ExtrasUnderstand which .NETStandard version is suitable for you here. In nutshell, the lower version supports more platforms while the higher version supports more APIs.
ExtrasGet a sample source code to jump start your development with .NET Standard 2.0.
Create Cross Platform Mobile App with .NETStandard2.0Marvin HengTwitter : @hmhengBlog : http://hmheng.azurewebsites.netGithub: https://github.com/hmheng