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:
- 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.
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.
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. Find the XML-node ItemGroup, that countains .cs-files and add the following line:
Save and return to Visual Studio. You should see something like this:
and after that, the SharedClass.cs appears in your 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: 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: 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:
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:
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