Following is a summary of the session “3-Screen Coding: Sharing code between Windows Phone, Silverlight, and .NET” presented by Shawn Burke, Architect on the Silverlight team, in PDC 2010.
- Sharing code is hard
- Sharing UI is harder
Writing portable code has a rocky past..
The whole “Write-Once-Run-Everywhere” theme didn’t work so much.
Some principles for sharing code between .NET platforms
- Don’t fragment the platform!
- Code-Portability is not good enough – need to be able to share binaries.
- Good user-experience requires specific UI suitable to the device. This actually challenge the very idea of sharing UI between platforms.
- Things that are the same should stay the same. For example, string.Compare should work the same both on web desktop and phone.
Why can’t I just run my app?
- Some API are very platform specific (System.Windows.Browser)
- Screen size is very different
- Input methods are very different
- Performance characteristics are different
Use assembly portability to share Component/Logic.
In .Net 4.0 you can write a class library in Silverlight and use it in .NET 4 application.
Using this technique we can create assemblies that contains most of the logic:
- Business Logic
- Validation Code
- File/Protocol processors
- Service Access
- View Models
- Authentication Code
Announcing: The Portable Library Project
This project will be available as a preview on H1 2011.
It will be a new class library type, that you can choose the targeted platforms and it will narrow the API you use to the APIs that are common between the targeted platforms.
The DLL that will be produced is a simple Silverlight DLL.
Can We Share Assemblies Today?
Yes! You can create a Silverlight 3 class library project today and have it reference from WPF, Silverlight web application and Silverlight Windows Phone application.
On the last case you will get a warning from Visual Studio but this should work nevertheless.
The downside is that you don’t get the automatic IDE support that minimizes the valid APIs, so you need to be careful not to use APIs that are not available in one of the targeted platforms.
That’s it for now,