Forum Cross Platform with Xamarin

WPF Application & Xamarin.Mac application in the same solution?

DanSmith.1531DanSmith.1531 USMember ✭✭

I currently work for a company that does WPF applications for our customers. We have been receiving lots of requests for Macintosh compatible applications, and I have a Xamarin.Mac license.

For the most part, we did a really good job of separating the view from the model in our applications. So, I created a brand-new Xamarin.Mac Cocoa project in Xamarin.Studio, copied over all of our shared code, created my Mac-specific UI and linked everything together. For the most part, this has worked very well.

However, we currently keep our Mac application in its own solution, and in its own source control system. So, while I was porting the code over to the Mac, the Windows team was finding and fixing bugs in the Windows application. Each time this happens, I need to manually port those code changes over to the Mac. Needless to say, it's not a fun or productive experience.

So, my "dream" situation would be to have the Mac application code be stored in the same .sln as the Windows application. That way, all of the common code will always stay in sync. The Visual Studio solution seems to be capable of being laid out this way:

-> Applications
--> App1
--> App2
-> Shared Code
--> Common Logging
--> Common Utils
--> etc

The only thing is that the shared code isn't 100% shared. There are a few minor differences. For example, when I'm creating a log file, I want to put information about the computer that is running the application. One of the items is "OSVersion". On Windows, I'm able to use the "Environment.OSVersion.ToString()" method to get a unique identifier for the Windows OS, however, when executed on a Mac, that doesn't give me the information I desire. So, I have a method that opens up a plist (/System/Library/CoreServices/SystemVersion.plist) and parses the data in it to figure out my OS. (I'd guess 95% of the code is re-used without any modifications)

Is anyone else able to re-use the same .sln file for both Windows & Mac C# applications? Any ideas on how to handle the "almost the same" shared code?


  • adamkempadamkemp USInsider, Developer Group Leader mod

    First, on the issue of sharing a .sln, a practical issue you will face is that the WPF .csproj won't be loadable by Xamarin Studio, and the Xamarin.Mac .csproj won't be loadable by Visual Studio. We've found it works better to have separate .sln files for each platform, but those .sln files may contain some of the same .csproj files. If you are using Portable Class Libraries (PCLs) then you can still work on the same files and the same projects for the shared components, but the platform-specific projects will only be used by that platform's .sln.

    As for the platform differences, there are two approaches. One is to use dependency injection for anything platform-specific. There are a million dependency injection frameworks out there of varying complexity. Google for "inversion of control" or "dependency inject" to see some options. Ideally you would choose one that works well with PCLs.

    Another approach is to not use PCLs and instead use either shared projects or just separate .csproj files that happen to share the same source files. We use the latter approach a lot. In either of these options you can use #if code to switch out implementations depending on the platform.

    You definitely want to try to converge on one of these approaches because obviously, as you're discovering, working out of entirely different codebases is not sustainable. Hopefully this gives you some clues to help you decide which route is best.

  • DanSmith.1531DanSmith.1531 USMember ✭✭

    Thanks for the response. I wasn't sure how other people were managing this situation. I need to sit down and talk with the team on Tuesday to figure out the best approach...

  • We are porting WPF application on Xamarin Android

Sign In or Register to comment.