- Hands-On Mobile Development with .NET Core
- Can Bilgin
- 499字
- 2021-06-24 13:55:31
Using .NET Standard with Xamarin
Even though the Xamarin and/or .NET Core target platforms (Platform APIs) are treated as if they have the same setup, capabilities, and functionalities as a platform-agnostic framework (.NET Standard), each of these target platforms are different from each other. The adaption layer (implementation of .NET Standard) allows us, developers, to treat these platforms in the same way.
Before the unification and standardization of .NET modules, together with shared projects, cross-platform compatibility was maintained by common denominators of implemented functionality on target platforms. In other words, the available APIs on each selected platform made up a profile that determined the subset of functionality that could be used for these platforms. These platform-agnostic projects that were used to implement the application logic were then packaged into so-called Portable Class Libraries (PCLs). PCLs were an essential part of cross-platform projects, since they could create and share application code that would be executed on multiple platforms:
Nowadays, since .NET API implementations on various platforms have all converged into (almost) the same subset, a standard set of .NET APIs were defined as the common implementation ground for cross-platform implementation – .NET Standard. As a simple analogy, .NET Standard can be considered the interface that's used to access the platform APIs which are implemented by target platform runtimes.
Using .NET Standard, we can replace the shared projects in our previous examples for both Xamarin.Forms and Xamarin classic applications. This would allow us to create a testable set of platform-agnostic interfaces which can be tested as a standalone library, unlike the shared projects, which are simply made up of a bundle of linked source code files. While shared source code files are directly compiled into the target platform projects, .NET Standard libraries are simply assigned as reference assemblies.
In order to replace the shared project with a NET Standard library in the Xamarin.Forms application, we would have to do the following:
- Create a .NET Standard Library project.
- Add the Xamarin.Forms NuGet package with the same version as the platform projects.
- Copy the App.xaml (and App.xaml.cs) file and added views to the Standard library.
- Reference the Standard library instead of the shared one:
Once these steps are completed, our Xamarin.Forms application would be using .NET Standard to share code between the platforms. However, this would be problematic for the conditional compilation symbols we introduced in our ViewModel, which return the platform string according to the application we are compiling ( iOS or Android):
What's happening here is that iOS and Android compilation symbols are only applicable to the platform-specific projects, so, unless you manually introduce these into platform-specific build configurations, they will not be available in .NET Standard projects. In order to remedy this, we can use a simple conditional to check the current runtime. (Alternatively, we can introduce an interface that will be used to inject the platform-specific implementations.):
public string Platform
{
get
{
if(Device.RuntimePlatform.Equals(Device.Android))
{
return "Android";
}
else
{
return "iOS";
}
}
}