When we worked on our Lauffeuer-project, we had some parts of our code, that we wanted to use on a Windows Phone 7 (= Silverlight Application) and on our server (normal console application). There is an extention for Visual Studio called Portable Class Libraries which includes these assemblies:

  • mscorlib.dll
  • System.dll
  • System.Core.dll
  • System.Xml.dll
  • System.ComponentModel.Composition.dll
  • System.Net.dll
  • System.Runtime.Serialization.dll
  • System.ServiceModel.dll
  • System.Xml.Serialization.dll
  • System.Windows.dll (from Silverlight)

The more platforms you want to serve, the less namespaces are avaliable. And, as you may have seen, System.Data.Linq is missing! And we love LINQ and we want to use it!

We had to make a workaround: Linking files with their relative path between projects.

1. Create multiple Projects in a Solution

I’ve creted a Solution, which contians a Console Application-Project and a Silverlight Application-Project, which is embedded in a Website. SolutionExplorer containing three projects: SomeApplication, SomeSilverlightApplication, SomeSilverlightApplication.Web

2. Add some code, that you want to share among platforms

I’ve added a class called sharedCode.cs, which is in a new namepace and in a seperate folder, to keep track of all the code.

The SharedClass.cs file in the project _SomeApplication_

3. Link fole to other projects

!Make sure you saved all files (Strg + Shift + S)!

Navigate to the Silverlight-project with your explorer. You’ll find the Visual C# Project file, with the csproj extention. Open it with a texteditor. The part of the csproj-file that contains all sourcefiles, that will be compiled Find the XML-node ItemGroup, that countains .cs-files and add the following line:

	<Compile Include="..\SomeApplication\SharedCode\SharedClass.cs" />

Save and return to Visual Studio. You should see something like this:

Visual Studio asks if you want to reload the project file

and after that, the SharedClass.cs appears in your Silverlight-project

SharedClass.cs is listed in SolutionExplorer inside the Silverlight project

4. Handling the file correctly

If you open the file, or jump into it because the debugger stopped there, you’ll see something like this: Warning: "This document is opened by another project" To really open the file in the “version” you want to see, you have to close it and reopen it via the SolutionExplorer.

5. Using Precompiler branches

If you’ve ever looked inside the Build Properties for a .NET-project, you’ll know this: Conditional compilation symbols There are different symbols for different platforms, and they are very useful.

Let’s say we want to extend our SharedClass.cs: We want to save logfiles to the device. As you know, there is this so-called IsolatedStorage for Silverlight applications, but we dont’ have them in normal applications. We write a method WriteLogFile, which opens a filestream for us, so we can write some logging-stuff. The kind of filestream is determined by the compiler symbols: Conditional compilation symbols in action: using if, else and endif

You see this file, as i’ve seen it after I’ve opened via the Console-project. If you open it via the Silverlight-project, it looks like this: Alternative view

6. Further Problems

You’ll have further problems with linked files, if you wokr a lot with them. For example:

  • double checkouts with TeamFoundationServer
  • funny dependencies among projects
  • twice as much unit-test projects
  • tests-projects, that fail on one platform, but work on the other I hope you have fun!

blog comments powered by Disqus

Published

12 August 2012

Tags