Xamarin 共享库和 PCL [英] Xamarin Shared Library and PCL

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

问题描述

xamarin 共享项目和可移植类库之间的确切区别是什么?

What is the exact difference between xamarin shared project and portable class library?

什么时候用共享库,什么时候用可移植类库?

When to use shared library and when to use portable class library?

是否可以在共享项目中编写本机功能,例如显示警报、访问相机并将其用于 android 和 iOS?

Is this possible to write native functionality in shared projects like showing alert,accessing camera and use it for both android and iOS?

谁能帮我解释一下.

推荐答案

  • 共享项目中,将为每个目标(Android、iOS、Windows Phone 等)编译每个代码文件.您可以使用 #if 编译器指令来包含特定于平台的代码.当您想访问相机时,您需要在 #if 块中为所有目标平台编写访问代码.这可能会弄乱您的代码,但可以更轻松地找到不同的实现.了解更多:http://developer.xamarin.com/guides/cross-platform/application_fundamentals/shared_projects/
  • 可移植类库 (PCL) 是针对通用 .NET 子集编译的,该子集与您想要的所有平台兼容.所以你可以访问 System.Net.Http 但你不能访问任何平台特定的代码.如果要访问 PCL 代码中的摄像头,则需要通过依赖注入通过通用接口访问它.有一些非常好的框架可以帮助您实现这一目标.其中最著名的一个是 MVVMCross (https://github.com/MvvmCross/MvvmCross/wiki).了解有关 PCL 的更多信息:http://developer.xamarin.com/guides/cross-平台/application_fundamentals/building_cross_platform_applications/sharing_code_options/#Portable_Class_Libraries
    • In shared projects each code file will be compiled for each destination (Android, iOS, Windows Phone etc). You are able to include platform specific code by using #if compiler directives. When you want to access the camera you need to write the access code inside an #if block for all destinated platforms. This can mess up your code but it can be easier to find the different implementations. Learn more: http://developer.xamarin.com/guides/cross-platform/application_fundamentals/shared_projects/
    • Protable Class Libraries (PCL) are compiled against a general .NET subset which is compatible to all platforms you want. So you can access System.Net.Http but you cannot access any platform specific code. If you want to access the camera inside the PCL code then you need to access it by a generalized interface via dependency injection. There are some pretty good frameworks helping you to archieve this goal. One of the most famous is MVVMCross (https://github.com/MvvmCross/MvvmCross/wiki). Learn more about PCL: http://developer.xamarin.com/guides/cross-platform/application_fundamentals/building_cross_platform_applications/sharing_code_options/#Portable_Class_Libraries
    • 我个人更喜欢 PCL,因为代码在没有任何编译器指令的情况下更容易阅读.使用 MVMCross,您可以通过 NuGet 使用大量插件.因此,您无需为相机访问、显示警报等编写自己的类.

      I personally perefer PCLs because the code is much easier to read without any compiler directives. Using MVVMCross you are able to use plenty of plugins via NuGet. So you don't need to write your own classes for camera access, showing alerts etc.

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

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