简体   繁体   English

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

[英]Cross platform class library for Mac and Windows using C#

I'm about creating an application which should run on Mac as well on Windows and should be deployed to the respective stores.我要创建一个应用程序,它应该在 Mac 上运行,也应该在 Windows 上运行,并且应该部署到相应的商店。 The GUI will be a HTML-based GUI hosted locally in a WebView control. GUI 将是在 WebView 控件中本地托管的基于 HTML 的 GUI。 The GUI will mainly communicate with the rest of the system using simple Json messages. GUI 将主要使用简单的 Json 消息与系统的其余部分进行通信。

The main logic is the same for both platforms and I would like to share as much code as possible.两个平台的主要逻辑是相同的,我想分享尽可能多的代码。 The idea is to use Xamarin on Mac and .NET on Windows and build a common class library which can be shared.这个想法是在 Mac 上使用 Xamarin,在 Windows 上使用 .NET 并构建一个可以共享的公共类库。

What should be the target of this shared assembly (PCL, netstandard or have different project files for Mac and Windows and just share the source files?)这个共享程序集的目标应该是什么(PCL、netstandard 或者有不同的 Mac 和 Windows 项目文件并且只共享源文件?)

I'd use .NET standard for all new projects, because it will replace PCLs in long term.我会在所有新项目中使用 .NET 标准,因为它会长期取代 PCL。 See paragraph As a library author, what should I do now?见段落As a library author, what should I do now? on https://blogs.msdn.microsoft.com/dotnet/2016/09/26/introducing-net-standard/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.作为库作者,您应该考虑切换到 .NET Standard,因为它将取代面向多个 .NET 平台的可移植类库。

In case of .NET Standard 1.x the set of available APIs is very similar to PCLs.对于 .NET Standard 1.x,可用 API 集与 PCL 非常相似。 But .NET Standard 2.x will have a significantly bigger API set and will also allow you to depend on libraries targeting .NET Framework.但是 .NET Standard 2.x 将拥有更大的 API 集,并且还允许您依赖面向 .NET Framework 的库。

The key differences between PCLs and .NET Standard are: PCL 和 .NET Standard 之间的主要区别是:

  • Platform tie-in.平台对接。
  • Platform availability.平台可用性。
  • Library availability.图书馆的可用性。

I'd use a shared library (code file sharing) only if I have to have small pieces that are platform specific.仅当我必须拥有特定于平台的小块时,我才会使用共享库(代码文件共享)。 But usually you should just create an interface and implement the platform specifics in classes that are implementing the interface.但通常您应该只创建一个接口并在实现该接口的类中实现平台细节。 And then use dependency injection to get the actual implementation in your cross platform library.然后使用依赖注入来获取跨平台库中的实际实现。

I advice you to use PCL .我建议你使用PCL I have apps for Windows 10(Net Core), for Android and iOS(Xamarin Forms).我有适用于 Windows 10(Net Core)、Android 和 iOS(Xamarin Forms)的应用程序。 These apps use the PCL library.这些应用程序使用 PCL 库。 It is the best way.这是最好的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM