简体   繁体   中英

Can I target iOS and Android only with a Xamarin PCL and have more .NET features?

I need to create a library in Xamarin that targets iOS and Android only. I do not need to support any other platforms, but I need as much .NET features as possible.

I see that in Change Targets dialog there is no way to uncheck Silverlight check-box:

在此输入图像描述

Can I create a Xamarin portable class library that targets iOS and Android only without Silverlight support and have more .NET features supported?

EDIT: Here are some classes that I need to support: X509CertificateCollection, SerializationInfo, NameValueCollection. I also need System.Web.Services namespace support. It looks like I can use all these in iOS and Android projects directly. Can I create PCL with all these features?

Profile24, which includes Silverlight is the narrowest you are going to get while include Xamarin.iOS and Xamarin.Android

FYI: I totally agree with Matt's suggestion of using Profile111 if you are going the PCL library direction. Sometimes for our projects, it is "faster" ($/time) to just go with "Shared Projects" and use #if/#else/#end in the shared code when needed. PCL libraries are great for sharing, but if you do not need the heartache of leaving framework pieces behind, the shared project direction can solve today's problem today... ;-)

As of the Xamarin 4.1 release two new profiles were added:

Profile 44  (.NET Framework 4.5.1, Windows 8.1) (netstandard 1.2)
Profile 151 (.NET Framework 4.5.1, Windows 8.1, Windows Phone 8.1) (netstandard 1.2)

Old ones:

Profile 5   (.NET Framework 4,     Windows 8)
Profile 6   (.NET Framework 4.0.3, Windows 8)
Profile 7   (.NET Framework 4.5,   Windows 8)
Profile 14  (.NET Framework 4,     Silverlight 5)
Profile 19  (.NET Framework 4.0.3, Silverlight 5)
Profile 24  (.NET Framework 4.5,   Silverlight 5)
Profile 37  (.NET Framework 4,     Silverlight 5, Windows 8)
Profile 42  (.NET Framework 4.0.3, Silverlight 5, Windows 8)
Profile 47  (.NET Framework 4.5,   Silverlight 5, Windows 8)
Profile 49  (.NET Framework 4.5,   Windows Phone Silverlight 8)
Profile 78  (.NET Framework 4.5,   Windows 8, Windows Phone Silverlight 8)
Profile 92  (.NET Framework 4,     Windows 8, Windows Phone 8.1)
Profile 102 (.NET Framework 4.0.3, Windows 8, Windows Phone 8.1)
Profile 111 (.NET Framework 4.5,   Windows 8, Windows Phone 8.1)
Profile 136 (.NET Framework 4,     Silverlight 5, Windows 8, Windows Phone Silverlight 8)
Profile 147 (.NET Framework 4.0.3, Silverlight 5, Windows 8, Windows Phone Silverlight 8)
Profile 158 (.NET Framework 4.5,   Silverlight 5, Windows 8, Windows Phone Silverlight 8)
Profile 225 (.NET Framework 4,     Silverlight 5, Windows 8, Windows Phone 8.1)
Profile 255 (.NET Framework 4.5,   Silverlight 5, Windows 8, Windows Phone 8.1)
Profile 259 (.NET Framework 4.5,   Windows 8, Windows Phone 8.1, Windows Phone Silverlight 8)
Profile 328 (.NET Framework 4,     Silverlight 5, Windows 8, Windows Phone 8.1, Windows Phone Silverlight 8)
Profile 336 (.NET Framework 4.0.3, Silverlight 5, Windows 8, Windows Phone 8.1, Windows Phone Silverlight 8)
Profile 344 (.NET Framework 4.5,   Silverlight 5, Windows 8, Windows Phone 8.1, Windows Phone Silverlight 8)

http://danrigby.com/2014/05/14/supported-pcl-profiles-xamarin-for-visual-studio-2/

While you will be limited if you try to target PCL, another option is to use a Bait-and-Switch NuGet, which would be setup as follows:

  • Shared Project - Contains all your shared code
  • Android Library Project - References Shared Project, so it gets all the code
  • iOS Library Project - References Shared Project, so it gets all the code
  • PCL Project - Empty project, which only contains empty stubs for the methods

Then you create a NuGet, where the Android payload is the Android Library project, and the iOS payload is the iOS Library project.

The PCL project then only surfaces the API that is PCL compatible, but the implementation happens to use everything you need.

For bait-and-switch approach see: http://log.paulbetts.org/the-bait-and-switch-pcl-trick/

PCL profile is (nearly) the same, including or not Silverlight, you have only acces to the PCL assemblies.

But, Xamarin Forms already allows you to cope with that, on each platform you have a .Droid and .iOS project, and on these projects you can use the full .net framework.

To access the code from these projects you have the Dependency Services , and if you want to use the same code for both, Android and iOS you have shared projects .

So basically, put all your shared coded in a shared project, implement an interface, register it as a dependency service, retrieve it in your Forms code and in this way you will get support for the full framework.

As the text tells you, it will automatically add silverlight because there will be no cut in functionality. So there will be no more features available.

On the other side, I would propose using Profile111 (see list in SushiHangover's answer). On the one side this allows you later adding Windows 10 as a target platform and also most PCLs seem to support this profile, since Silverlight is going to die (Silverlight 5 was last and Windows Phone is UWP)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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