EDIT: See my own replies below.
My question's reason for existence:
I'm working on a Xamarin cross platform app (using PCL + forms), and I need to use System.Security.Cryptography and System.Net.Sockets. My PCL does not let me use either of these. My PCL is profile 78. I only intend to develop for mobile devices (iOS, Android) in particular. Windows phone would be a cherry on top, but is definitely not a requirement. I know that Android and iOS support both of these namespaces. I could simply use the DependencyService and put the same exact file in both my platform ios and platform droid folders, but then every time I edit one I would have to copy paste to the other.
My question:
How can I either:
A. Modify my PCL to let me use System.Net.Sockets and System.Security.Cryptography
B. Use the same C# file for both iOS and Android (i.e. my AES class or my Communications class that I currently believe I have to copy-paste in two places).
C. Some other alternative that you may notice while reading this post.
(tl;dr; you can find PCL-friendly libraries for sockets and crypto here)
Hi @CollinIrwin
The general preference when doing cross-platform Xamarin is to use a PCL over a shared project. A PCL limits the .NET API that you can code against to a subset of .NET that is supported by the platforms you choose to target, however, the actual platforms still execute against their native .NET or WinRT platform at runtime. The core reason behind many of the exclusions from PCL profiles is that the WinRT framework used by WP and Windows Store has several APIs that differ substantially from the .NET framework surfaces.
There is a pattern colloquially referred to as 'Bait and Switch' that can be used to expose platform-specific functionality and non-homogenous APIs via a unified PCL-accessible API. You can read about how it works here, but for the end user, it is as simple as installing the appropriate NuGet package into your PCL and platform projects, and coding against it from your PCL project.
There are a large number of existing plugins catalogued here. Fortunately, there is both a crypto plugin, and a sockets plugin available already. I have not used the crypto one, but I maintain the sockets one, so give it a go and if you have any questions, let me know!
Answers
Well... I think I found the answer to using the same C# file in multiple platforms, or rather the solution to my ultimate question of using System.Crypto and System.Sockets:
http://developer.xamarin.com/guides/cross-platform/application_fundamentals/shared_projects/
For some reason I didn't think you could do both PCL and Shared in the same solution. I was wrong there. I also didn't realize that the option for making these directories was under the Add New Project - > .Net, I expected it to be under Cross-Platform.
I still don't know how to modify my PCL's assemblies, but that isn't needed any more so I'll push that off for later.
Okay so now I have a PCL, with viewmodels for Xamarin.Forms, a shared project, and platform projects.
The PCL's viewModels need to make use of the classes in the shared project, but I must be missing something as I am getting "The type or namespace could not be found. Are you missing an assembly reference?
But I can't include the reference because then my shared project loses its ability to access System.Net.Sockets and System.Security.Cryptography
I'm beginning to feel like it would be easier to just make the whole project a shared project rather than messing around with part PCL/part shared project. I am also seemingly losing understanding of why PCL is useful in the first place. Is it purely a tool that limits you to only a specific subset of .NET?
(tl;dr; you can find PCL-friendly libraries for sockets and crypto here)
Hi @CollinIrwin
The general preference when doing cross-platform Xamarin is to use a PCL over a shared project. A PCL limits the .NET API that you can code against to a subset of .NET that is supported by the platforms you choose to target, however, the actual platforms still execute against their native .NET or WinRT platform at runtime. The core reason behind many of the exclusions from PCL profiles is that the WinRT framework used by WP and Windows Store has several APIs that differ substantially from the .NET framework surfaces.
There is a pattern colloquially referred to as 'Bait and Switch' that can be used to expose platform-specific functionality and non-homogenous APIs via a unified PCL-accessible API. You can read about how it works here, but for the end user, it is as simple as installing the appropriate NuGet package into your PCL and platform projects, and coding against it from your PCL project.
There are a large number of existing plugins catalogued here. Fortunately, there is both a crypto plugin, and a sockets plugin available already. I have not used the crypto one, but I maintain the sockets one, so give it a go and if you have any questions, let me know!