I can't add System.Net.Sockets
to a Xamarin PCL project can this be done using "Bait and switch" AKA advanced PCL approach?
System.Net.Sockets
is available for Xamarin.iOS and Xamarin.Android projects
Yes, this is possible. The approach we use is to make separate iOS and Android Class libraries that will hold non-UI type code that is not accessible from PCL, and have that code implement interfaces that are accessible from the shared PCL layer.
For example:
Create ISocketHandler in Shared PCL project, and it has OpenConnection method.
Create an iOS.Framework class library, and in it create an iOSSocketHandler class that implements ISocketHandler and has platform specific code on using sockets in Xamarin iOS. Do the same for Android.
Then you need dependency injection so that whenever your shared code asks for ISocketHandler, they get the appropriate implementation. We use Unity, and in our Main.cs we will register like so:
IocContainer.GetContainer().RegisterType<ISocketHandler, iOSSocketHandler>();
Then in your shared code you can request the implementation like so:
IocContainer.GetContainer().Resolve<ISocketHandler>();
There are other strategies that can accomplish what you want, but this is what we use in most scenarios.
Also, before trying to do this yourself, you may want to check this out https://github.com/rdavisau/sockets-for-pcl
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.