简体   繁体   English

为Xamarin项目编译C#库

[英]Compiling C# library for Xamarin project

I'm pretty new to Xamarin and the whole C#/.NET world. 我对Xamarin和整个C#/。NET世界还很陌生。 I'm planning to develop a cross plateform app using Xamarin, and I will need to create libraries to share with others developers. 我计划使用Xamarin开发跨平台应用程序,并且需要创建库以与其他开发人员共享。 I've heard of PCL, but some of the limitations bug me a bit. 我听说过PCL,但是有些限制使我有些困惑。 For exemple, I can't use System.Net, even if I can use it directly in an iOS or an Android project. 例如,即使我可以直接在iOS或Android项目中使用System.Net,也不能使用System.Net。

I know that, again for exemple, System.Net is compile differently in an iOS project than in an Android project, and that's one of the reason I can't use it in a PCL. 我知道,举例来说,System.Net在iOS项目中的编译与在Android项目中的编译是不同的,这就是我不能在PCL中使用它的原因之一。 However, is it possible to write a library, and then compile it for each plateform? 但是,可以编写一个库,然后为每个平台编译它吗?

Shared projects, on the other hand, let me do it. 另一方面,共享项目让我去做。 But I can't share it as a library. 但是我不能作为图书馆分享。

So, what I'm trying to do is to have only one code to maintain. 所以,我想做的是只维护一个代码。 Write once, compile twice, run everywhere (on mobile, except Windows Phone). 编写一次,编译两次,到处运行(在移动设备上,Windows Phone除外)。

So, the question is : 因此,问题是:

Is it possible to achieve such a thing? 是否有可能实现这样的目标?

In our Desktop/Android/IPhone projects we used next approach: 在我们的Desktop / Android / IPhone项目中,我们使用了以下方法:

Assume we have next projects 假设我们有下一个项目

  • Droid 机器人
  • Touch 触摸
  • Common.PCL 普通PCL
  • Common.Shared 共同共享

Droid and Touch projects reference PCL and Shared libraries. Droid和Touch项目参考PCL和共享库。 Next, create an interface in Common.PCL 接下来,在Common.PCL中创建一个接口

namespace Common.PCL
{
    public interface INetSpecificFactory
    {
        INetSpecific CreateSomething();
    }
}

and implementation of it in Shared 并在共享中实现

using Common.PCL;

namespace Common.Shared
{
    public class NetSpecificFactory : INetSpecificFactory
    {
        public INetSpecific CreateSomething()
        {
            ....
        }
    }
}

Add autofac module in Shared library 在共享库中添加autofac模块

using Common.PCL;
namespace Common.Shared
{
    public class AutofacModule : Module
    {
        protected override void Load(ContainerBuilder builder)
        {
            builder.RegisterType<NetSpecificFactory>().As<INetSpecificFactory>();
        }
    }
}

So, I can use INetSpecificFactory in my PCL via DI. 因此,我可以通过DI在PCL中使用INetSpecificFactory。 It's better than use link files (it was one of the options at first) 比使用链接文件更好(它最初是选项之一)

What you could do is use compiler directives to differentiate which platform you are developing for.. unfortunately this does not reduce the fact to write different code twice for the different platforms as you need different libraries doing the same stuff on the different platforms. 您可以做的是使用编译器指令来区分您要开发的平台。不幸的是,这并不能减少为不同平台编写两次不同代码的事实,因为您需要不同的库在不同平台上执行相同的工作。

With these directives you could compile each or your own library for the specified platform. 使用这些指令,您可以为指定平台编译每个或您自己的库。

.NET Core and .NET Platform Standard are coming and you should check them out. .NET Core和.NET Platform Standard即将推出,您应该检查一下。 They are the future to replace PCL. 它们是取代PCL的未来。

I have already ported #SNMP Library partially to .NET Core RC1 and hope migrate the rest when RC2 and later come, 我已经将#SNMP库部分移植到.NET Core RC1上,希望在RC2及以后的版本中将其余的移植,

https://blog.lextudio.com/2016/03/how-to-port-snmp-library-to-net-core/ https://blog.lextudio.com/2016/03/how-to-port-snmp-library-to-net-core/

What you should do is creating a Class Library for both iOS and Android. 您应该为iOS和Android创建类库。 Then, you write your code in only one of them, and link the code files to the other Class Library. 然后,仅在其中一个代码中编写代码,并将代码文件链接到另一个类库。 That way, you're writing your code once, compile each Class Library and you get 2 DLL library from the same code. 这样,您只需编写一次代码,编译每个类库,就可以从同一代码中获得2个DLL库。

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

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