While this question is a year old, I ran into a blog post that helps solve this problem: http://dotnetbyexample.blogspot.com/2014/05/writing-behaviors-in-pcl-for-windows.html -- This blog contains a download sample as well, so you can examine its inner workings. The blog works with the IBehavior
interface, but the conept is the same.
Basically, the technique is called the "NuGet bait and switch"; you would need to create a NuGet package that contains a PCL library and a platform-specific library (one for each platform).
Let's imagine that your PCL is going to be called "AwesomeMvvm". You would need:
- AwesomeMvvm (Portable Class Library)
- AwesomeMvvm.Silverlight
- AwesomeMvvm.Windows81
- ...
When you attempt to use the ICommand
interface in your portable class library (PCL), you will get an error stating Cannot resolve symbol ICommand
. To solve this, simply create an interface making sure it is in the same namespace System.Windows.Input
. Your portable class library can now compile!
Now that your portable class library is compiled, create your platform-specific libraries and add a reference to your portable class library to them. Each of these libraries only need one code file that contains the TypeForwardedToAttribute
as an assembly-level attribute:
[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Windows.Input.ICommand))]
The final step is to create a single NuGet package containing the portable class library, as well as each platform-specific library. If you did everything correctly, add a reference from your NuGet feed and everything should work just fine. NuGet will prefer the platform-specific libraries whenever it can, each of which rely on the portable class library. The trick is that at runtime it will use the native version of ICommand
because you specified to forward your portable class library version to be forwarded to the platform-specific version.