Frequently Asked Questions

Where's [Enter name here] API?
File a bug and we'll add it to the backlog.

Can I share the types from PclContrib with my platform-specific projects?
No, not currently. While the types in PclContrib look and feel like their platform-specific counterparts, the runtime and compiler will see them as completely different types. While we have some ideas on how to make this work, this is a feature that we won't be looking at for the short term.

How does this PclContrib work under the covers?
There are two types of APIs in PclContrib:
  • Fully portable: These are types (such as BackgroundWorker) that have been completely rewritten in "portable" code and do not require the platform-specific adapter assemblies (such as Portable.Desktop) to be present alongside the application. The source code for these types can be copy and pasted easily into existing projects.
  • Portable adapters: These are types (such as HMACSHA256) that underneath use adapters that delegate to platform-specific types. These require the platform-specific adapter assemblies to be present alongside the application. The first time you attempt to use one these APIs, Portable.Runtime will automatically find and load the appropriate platform-specific adapter assembly for which it will delegate the calls onto.

Last edited Feb 22, 2012 at 3:41 PM by davkean, version 3

Comments

Mickp2 Nov 19, 2013 at 5:08 AM 
Also, the button2_Click() can be in your form or non-PCL project.
The rest of the code can be placed in a PCL (or not).

Mickp2 Nov 19, 2013 at 4:20 AM 
The following .net code works on Desktop implementation.
First add references to Portable.Desktop and Portable.Security.Cryptography.ProtectedData
private void button2_Click(object sender, EventArgs e)
{
String encrypted = PCL.CentralClass.Encrypt("yo");
String decreypted = PCL.CentralClass.Decrypt(encrypted);
//PCL.CentralClass.
}
//http://stackoverflow.com/questions/12688296/portable-class-library-pcl-contrib-cryptography
//\Source\Portable.Security.Cryptography.ProtectedData\Security\Cryptography\ProtectedData.cs

static byte[] GetBytes(string str)
{
byte[] bytes = new byte[str.Length * sizeof(char)];
System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);
return bytes;
}

static string GetString(byte[] bytes)
{
char[] chars = new char[bytes.Length / sizeof(char)];
System.Buffer.BlockCopy(bytes, 0, chars, 0, bytes.Length);
return new string(chars);
}

public static String Encrypt(String strEncrypt)
{
byte[] userData = GetBytes(strEncrypt);
byte[] optionalEntropy = null;
byte[] x = System.Security.Cryptography.ProtectedData.Protect(userData, optionalEntropy);
return GetString(x);
}
public static String Decrypt(String strDecrypt)
{
byte[] encryptedData = GetBytes(strDecrypt);
byte[] optionalEntropy = null;
byte[] x = System.Security.Cryptography.ProtectedData.Unprotect(encryptedData, optionalEntropy);
return GetString(x); ;
}