官术网_书友最值得收藏!

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:

  1. Create a .NET Standard Library project.
  2. Add the Xamarin.Forms NuGet package with the same version as the platform projects.
  3. Copy the App.xaml (and App.xaml.cs) file and added views to the Standard library.
  4. 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";
}
}
}

主站蜘蛛池模板: 肃宁县| 沙坪坝区| 建始县| 额敏县| 徐汇区| 莆田市| 利辛县| 枞阳县| 长治市| 乌拉特前旗| 宜昌市| 尤溪县| 乌拉特前旗| 定兴县| 永丰县| 盱眙县| 富平县| 成都市| 伊川县| 多伦县| 双流县| 廊坊市| 淮北市| 哈密市| 海兴县| 莲花县| 丹江口市| 巴楚县| 兰溪市| 山阴县| 阳东县| 刚察县| 上虞市| 广昌县| 孟州市| 贡觉县| 合水县| 泾源县| 甘孜| 海盐县| 延吉市|