12

I have a solution that contains both a Silverlight 4 and a WPF 4 solution. One is the web version of the app and one is the desktop version.

Both projects have similar domain classes and interfaces and both access the cloud for storage and other stuff.

I wanted to create a common Interfaces and Classes project, so I created a Class Library, but wouldnt you know it, Silverlight cannot add a reference to a Class Library. So I created the common project as a Silverlight Class Library, but when I reference that from the WPF project I get warnings:

Warning The project 'Interface.Common.Silverlight' cannot be referenced. The referenced project is targeted to a different framework family (Silverlight)

and I even get class loading runtime errors when I use any class from that library because "System.Windows cannot be found at runtime"

I want to share code between the two projects, how can this be achieved in a clean way?

Thanks for any help you can give

Mark
  • 14,820
  • 17
  • 99
  • 159
  • 1
    MSDN - Sharing Code Between Silverlight and WPF: http://msdn.microsoft.com/en-us/library/ff921109(v=pandp.40).aspx – Fredrik Hedblad Jan 06 '11 at 23:50
  • thanks, I am doing this now, but still have a lot of questions... such as can you share Azure access code between SL and WPF? – Mark Jan 07 '11 at 00:48
  • 2
    Attention searchers! Check out [Portable Class Libraries](http://msdn.microsoft.com/en-us/library/gg597391.aspx), now supported in .NET 4.0! –  Jun 15 '11 at 19:45

3 Answers3

7

Create two parallel projects one for WPF and one for Silverligth. For one of the projects, add the files as links instead of files.

I have done this sucessfully for the Microsoft Silverlight Analytics Framework with one code base for Silverlight 4, WPF, and Windows Phone 7.

Michael S. Scherotter
  • 10,715
  • 3
  • 34
  • 57
  • so create two Common.Interface projects (Common.Interface.SL and Common.Interface.WPF) and the files in one of these (which one?) will be links instead of actual files? What does this achieve? – Mark Jan 07 '11 at 00:29
  • In Project -> Add Existing Item notice the dropdown arrow next to the Add button. Download the MvvmLight Toolkit for an example of code-sharing between Sliverlight and WPF projects using linked C# files: http://mvvmlight.codeplex.com/ – Rick Sladkey Jan 07 '11 at 02:18
  • Yeah, this is the one I see most people doing. It doesn't matter which project has the files and which has the "linked items". Then if you have code that should only be one one platform it can be only in one project, and if a class should be just a bit different you can do things like #ifdef SILVERLIGHT, or have a partial class with a method/property/etc. defined differently in each platform's project in another partial of the same class. – Austin Lamb Jan 07 '11 at 04:57
  • 1
    so why not just have a shared Silverlight class library? is there much difference? – Mark Jan 07 '11 at 05:10
  • 1
    Because this way the Silverlight project compiles and links against the Silverlight BCL, and the WPF one compiles and links against the full .NET BCL, and it avoids the warning from VS (which matters more if you have warnings promote to errors during compilation - which is a great thing to do if you can). – Austin Lamb Jan 07 '11 at 06:16
  • Oh, and because it allows the flexibility I mentioned about having WPF-specific code in the WPF project and SL-specific code in the SL project, so you can share as much as possible, but add small tweaks or optimizations where the different platforms allow. – Austin Lamb Jan 07 '11 at 06:17
0

What about xaml sharing code with silverlight and WPF

joby james
  • 71
  • 3
0

Create a "Portable" project. that can be referenced from both WPF and Silverlight

Pingi
  • 342
  • 3
  • 11