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

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

问题描述

我正在尝试找出哪种方法更适合我们的项目.

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

Xamarin的PCL库或共享项目.

PCL Library or Shared Project by Xamarin.

Xamarin文档链接中,它表示经验法则是当我们不共享库时选择共享项目.共享项目可以用#if编写,以确保它可以在多个平台上使用-这还会导致重构#if无效的一些问题.

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天全站免登陆