Search code examples
windows-phone-7.net-4.0.net-3.5projects-and-solutionswindows-phone-8

How to organize Windows and Windows Phone projects to reuse existing code-base?


Fist of all I want to say that I've read these two discussions: How to organize Windows Phone code base to target both 7.x and 8 platforms and Windows Phone 7/WPF - Sharing a codebase. So, I know about Portable Class Librariy project type which might be an answer to my question. But it isn't an answer for my case.

Problem

I have some old code-base in my project WindowsClassLibraryNetFw35. I need this project to be a class library which targets .NET Framework 3.5. The reason for it is that some dependent applications are deployed on customers' machines which don't have .NET Framework 4+ installed. Unfortunately I can't force my customers to upgrade their environment.

Now I create new project(s) for Windows 8 and Windows Phone 8. As you know, Windows Phone 8 projects will fail when I attempt to reference my old library because it is not a compatible Windows Phone project (see screenshot). enter image description here

Question

What is the best way to reorganize my project type(s) in order to be able to support both my old and new projects?


Solution

  • I would still aim to get a PCL working, but if that fails, you can use the same code in two different projects.

    Create two different projects, one Windows Phone, the other a Windows Store App. Add a file to one, then add that other file as a link to the other project. This means you have a single source file between two different projects, but will be compiled with each respective runtime tools. Then you can do things like this in your code:

    void Foo()
    {
        #if WinRT
            //WinRT specific code here
        #elif WindowsPhone
            //Silverlight specific code here
        #endif
    }
    

    Just add the respective compiler constants on the Build tab of your Windows Phone and WinRT projects. This is far a more tacky solution, but sometimes its the only approach that works.