mkale.com

Windows Phone Code Sharing

I've been working on making two apps (lite and pro) based off a shared codebase. On iOS, I do this with a single Xcode project with two targets. I include most of the same source files in both targets and can build each one separately. For the few differences between the apps, I use compiler flags to #define a different constant in each target and can use different code as needed.

I tried to do something similar on Windows Phone. I created two separate .csproj files for the Lite and Pro projects, but had trouble including the same files in both of them. I wanted to include many of the same .cs source files but keep the .xaml different, and the files in the "Properties" directory different. (i.e., AssemblyInfo.cs and friends.) I wasn't able to rename the "Properties" directory and have it work correctly, nor was I able to references .cs files that lived outside of the directory containing the .csproj file correctly. It's possible I was doing something wrong here and there is a better approach, but this is what I came up with.

Top level directory contains .cs files for business logic, model classes, etc. As much as possible. It also contains the directories for each project, and the bulk of the code behind logic in files called (e.g.) "MainPageXamlShared.cs". It looks like this:

directory winphone/
Statistics.cs  (business logic)
Contraction.cs  (model class)
MainPageXamlShared.cs  (shared code-behind)
ContractionTimer/MainPage.xaml
ContractionTimer/MainPage.xaml.cs
ContractionTimer/Properties/...
ContractionTimer/ContractionTimer.csproj
ContractionTimerLite/MainPage.xaml
ContractionTimerLite/MainPage.xaml.cs
ContractionTimerLite/Properties/...
ContractionTimerLite/ContractionTimerLite.csproj
... more files of course

I'm leaving out a bunch of files but this gives you an idea. The MainPage.xaml in each directory are the full xamls (with differences between the two apps), but the code behind is entirely shared. MainPage.xaml.cs in each directory looks like this:

using Microsoft.Phone.Controls;

namespace ContractionTimer {
    public partial class MainPage : PhoneApplicationPage {
    }
}

That's it! All the logic is in the MainPageXamlShared.cs in the parent directory, which I was able to reference from each project with no trouble:

using Microsoft.Phone.Controls;
using System;
using System.Lots.More.Of.These;

namespace ContractionTimer {
    public partial class MainPage : PhoneApplicationPage {
        private readonly TimeSpan TIME_INTERVAL_SECONDS = TimeSpan.FromSeconds(1);
        // ...
        public MainPage() {
            InitializeComponent();
            // ...
        }
        // well, you get the idea ...
    }
}

This allows me to at least share the bulk of the code between two different versions of the app. A couple of final notes:

  • I know that Lite/Pro versions of apps are much less neeed on Windows Phone due to the very nice time-limited demo abilities of the platform. This works for many people but I wanted to do the traditional Lite/Pro versions for this app. That allows me to have a truly free (but ad supported) version that won't explode on people when they're in the middle of Labor. And my app is one that people might only use (intensly!) for a few hours and be done with, so the time-limited demo doesn't work as well here.
  • Please excuse my very basic understanding of Windows Phone. The other reason I started with a Lite (free) app is that I'm stil relatively new to the platform and don't (yet) want to make anyone pay for my Windows Phone apps. The last time I wrote serious code for a Windows platform, I was writing native C++ and COM based NT services that had no UI (but a lot of uptime!). I've got a lot of catching up to do in the last few years, including basically all of .NET, C#, Silverlight, XAML, and an alphabet soup of other things. I spend most of my time on iOS these days, what limited time I have after that on Android, and am attempting to work on Windows Phone here and there as a third platform. Visual Studio sure is nice though, that's one thing I've missed.
  • The method described above feels like a bit of a hack. If anyone knows of a better way to share code between a Lite and Pro Windows Phone app, please let me know!

History | Blog | mkale.com