Xamarin 共享项目与可移植类库 [英] Xamarin Shared Projects vs Portable class libraries

查看:35
本文介绍了Xamarin 共享项目与可移植类库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想弄清楚哪种方法更适合我们的项目.

I am trying to figure out which is the better way to go for our project.

PCL 库或 Xamarin 的共享项目.

PCL Library or Shared Project by Xamarin.

此处的 Xamarin 文档链接中,它说经验法则是当我们不共享库时选择共享项目.共享项目可以用 #if 编写,以确保它适用于多个平台 - 这也会导致重构 #ifs 不活动的一些问题.

In the Xamarin documentation link here it says that a rule of thumb is to choose shared project when we will not share the library. The shared project can be written with #if's to make sure it works with multiple platforms - this also causes some issues with refactoring #ifs not active.

但我有一种直觉,将这段代码放到共享类中是不对的.如果可用于 Windows、Android 和 IOS 移动平台的代码使用共享项目而不是 PCL - 这意味着我们在共享项目中使用 #ifs 而不是在平台特定项目中编写平台特定代码.

Yet I have a gut feeling it is not right to put this code to a shared class. If a code that is to be available to Windows, Android and IOS mobile platforms is using a shared project instead of PCL - it means we are using #ifs inside shared project instead of writing platform specific code in a platform specific project.

这是试图通过#ifs 支持非 PCL 项,并使共享代码更复杂且更难维护.这不应该是 Xamarin 为改进 .NET PCL 代码库而要做的工作吗?

This is trying to make support of non PCL items via #ifs and making the shared code more complex and harder to maintain. Shouldn't this be the work to be done by Xamarin for improving .NET PCL codebase?

这也意味着我们将平台特定的功能放在共享项目中,而不是平台特定的项目中——即将特定平台的复杂性从平台项目本身中隐藏——这在架构方面感觉是错误的.

And also this means we are putting platform specific features in the shared project and not the platform specific project - i.e. hiding complexity for a specific platform from the platform project itself - which feels wrong in terms of architecture.

我是对的(在那种情况下我与 Xamarin 文档相冲突)还是我遗漏了什么?

Am I right (in that case I am conflicting with Xamarin documentation) or am I missing something?

推荐答案

两者各有千秋.例如,您可以将一个接口放入 PCL,然后在共享代码中实现它,如果该实现具有相当数量的共享代码.

Both have their place. For example you could put an interface into a PCL, then implementation of it in shared code IF the implementation would have decent amount of shared code.

我也不喜欢编译器标志,我更喜欢使用部分类.通过这种方式,您可以避免使用大多数甚至全部编译器标志.Class1.cs 将进入共享项目,其余将进入他们的平台特定项目.

I don't like compiler flags either, I would prefer to use partial classes. This way you can avoid majority, or even all, of compiler flags. Class1.cs would go into shared project and the rest would go into their platform specific projects.

Class1.cs
Class1.ios.cs
Class1.android.cs
Class1.wp8.cs

这篇关于Xamarin 共享项目与可移植类库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆