Mac 和 Windows 使用 C# 的跨平台 class 库

Cross platform class library for Mac and Windows using C#

我要创建一个应用程序,它应该 运行 在 Mac 以及 Windows 上,并且应该部署到各自的商店。 GUI 将是基于 HTML 的 GUI,本地托管在 WebView 控件中。 GUI 将主要使用简单的 Json 消息与系统的其余部分通信。

两个平台的主要逻辑是一样的,我想分享尽可能多的代码。这个想法是在 Mac 上使用 Xamarin,在 Windows 上使用 .NET,并构建一个可以共享的通用 class 库。

这个共享程序集的目标应该是什么(PCL、netstandard 或 Mac 和 Windows 有不同的项目文件并只共享源文件?)

我建议你使用 PCL。我有 Windows 10(Net Core)、Android 和 iOS(Xamarin Forms) 的应用程序。这些应用程序使用 PCL 库。这是最好的方法。

我会为所有新项目使用 .NET 标准,因为从长远来看它将取代 PCL。 请参阅第 As a library author, what should I do now?https://blogs.msdn.microsoft.com/dotnet/2016/09/26/introducing-net-standard/

As a library author, you should consider switching to .NET Standard because it will replace Portable Class Libraries for targeting multiple .NET platforms.

In case of .NET Standard 1.x the set of available APIs is very similar to PCLs. But .NET Standard 2.x will have a significantly bigger API set and will also allow you to depend on libraries targeting .NET Framework.

The key differences between PCLs and .NET Standard are:

  • Platform tie-in.
  • Platform availability.
  • Library availability.

只有当我必须拥有特定于平台的小片段时,我才会使用共享库(代码文件共享)。但通常您应该只创建一个接口并在 类 中实现实现该接口的平台细节。然后使用依赖注入在你的跨平台库中得到实际的实现。