Project Linker - sharing single code base between Silverlight and Desktop applications

See WEBCAST <= familiarization with the ProjectLinker

The project linker permits you to easily share code and write reusable components for both the Silverlight and Desktop application platforms.  The benefit is that you can maintain a single codebase (and libraries) that will work on both platforms.  

I would recommend downloading the CompositeWPF/Prism application and reviewing the help information provided on the Project Linker.  In the Composite Application Guidance for WPF and Silverlight.CHM, if you'll search for "Project Linker", you will find a number of referenced links for more detailed information.  An excerpt from the "Multi-Targeting Technical Concept" link follows:

Silverlight and WPF are not binary compatible so code and components have to be recompiled for each target environment. The approach the Composite Application Guidance for WPF and Silverlight is taking is to provide guidance on structuring application and module code into multiple linked projects. Each project manages all of the references, resources, and code specific to the WPF or Silverlight target environment. Code that is shared is linked between two projects so that it is automatically compiled into each target. Unit tests can similarly be shared between the two environments, so that they can be written once and run against the target code in either of the two target environments. The Composite Application Guidance for WPF and Silverlight includes tooling to help create and maintain these links.

Non-UI code and components are probably going to be the easiest to share, so adhering to separated UI patterns are essential in making sure that the UI and non-UI pieces of the application or modules are cleanly separated.

The Project Linker essentially allows you to link two project so that whatever you do in the Source project automatically happens in the Target project.

The documentation will recommend that you use the least common denominator as your source, i.e., since Silverlight is a subset of WPF desktop if we code in Silverlight it will be supported on the Desktop side.   I follow their recommendation and use Silverlight as the source.

The first thing I'll do is create a Desktop class library project and a corresponding Silverlight class library project - the only difference between the two project names will be a suffix of .Desktop and .Silverlight.

Once my projects are completed I'll then go to the projects properties and remove the suffix from the default namespace - since we're sharing code on both sides they both have to have the same namespace; this is the only change we have to make to the project.

Now whenever I add a folder or class to the source (Silverlight) project it will automatically be made to the Desktop side.

Typically when you add a class it will automatically add default namespaces.  You'll want to remove these and only use the actual namespaces required because by default the Silverlight namespaces are not supported on the Desktop side.

Recommendation:  After adding classes - compile and compile often.   Anytime you add a reference to the Silverlight side be sure to add the same reference to the Desktop side - if the namespaces are different then use the #if SILVERLIGHT #Else #Endif conditional statements as applicable (I show an example in the WebCast).


Tags: ,
Categories:


Actions: E-mail | Permalink |  Grammar/Typo/Better way? Please let me know