Add support for WPF (Windows Desktop) in a Xamarin.Forms solution

JBeckJBeck USUniversity ✭✭✭

Tutorial Overview

In my latest tutorial we will create a cross-platform solution with Xamarin.Forms that includes WPF (Windows Desktop) as a target.
https://sites.google.com/site/netdeveloperblog/xamarin/forms/xamarin-forms-and-wpf

Cross-platform application development should reach the largest potential customer base it can. With Xamarin.Forms, you can create a solution with projects that target Android, iOS, Windows 10, Windows Mobile, Windows 8.1, and Windows Phone 8.1 out-of-the-box. Unfortunately, Windows Desktop is not included with this template. With so many people still using and relying on traditional Windows Desktop applications, many times it is important to provide a solution for that target environment.

Fortunately, with only a few modifications, we can refactor our default Xamarin.Forms solution to include a WPF project. Furthermore, we will establish a project structure that ensures all the projects can still utilized common code (things like View Models, Services, Models) from a PCL project within the solution.

We will cover the following activities:

  • Creating a new solution from the default Xamarin.Forms project template
  • Adding a Core PCL to our solution, to contain our common code
  • Add some common code to our Core PCL
  • Using a simple IoC container to replace Xamarin.Forms DependencyService class
  • Add a WPF project to our solution to target Windows Desktop
  • Implementing IHelloService in each platform
  • Creating some simple data binding in our MainPage and MainWindow views

Source Code

On GitHub: https://github.com/C0D3Name/XamFormsWpf
The source code for this tutorial can be found on my GitHub page.

Posts

  • BenColemanBenColeman USMember ✭✭
    edited August 2016

    @JBeck Thank you for the post. I have a question:

    I have an existing Xamarin.Forms solution which targets iOS and UWP, primarily. Is there a way to add WPF support to an existing XF project?

  • JBeckJBeck USUniversity ✭✭✭

    @BenColeman It is definitely possible.

    What you'll need to do is create a new PCL with the same profile as your existing Forms PCL. Add references to the new PCL from your existing projects. You can then migrate your Model and View Model layers to this new PCL. You won't be able to share your XAML views because WPF XAML != Forms XAML.

    You can then create your WPF project and have it reference the new PCL as well. It can reuse the Model and View Model layers.

    If you use DependencyService, you will need to replace it with an IoC container, I've used TinyIoC in the past, and it has worked well for me. You'll also need to implement navigation for WPF. The best approach would be to abstract your navigation to an Interface, then wire the implementation in Forms/WPF with IoC.

    If you are using an MVVM framework, there's good news and bad news. Frameworks like MvvmCross and ReactiveUI support both WPF and Forms, so the majority of the work will be implementing the framework components in your WPF project. If you are using a dedicated Forms MVVM framework, it may not have any support for WPF.

    If you are using a forms-specific MVVM framework, you will have the most work to do because you'll need to decouple from that framework in order to support WPF.

    Hopefully this will give you an idea of the level-of-effort for your specific solution.

  • markleavesleymarkleavesley USMember ✭✭

    I realise this is an old article but it's one of the few I can find on adding WPF to a XF solution. I'm looking at using Prism for XF (6.3.0) and given the historical association between Prism and WPF was rather surprised to find that the Prism target wizard does not mention WPF. Will this tutorial work for Prism as well or is it going to require something totally different? I am looking at creating a XF solution that targets iOS, Android UWP and classic desktop and we already have Prism experience in the company. I was hoping that Prism for XF would naturally include a WPF target!

  • NMackayNMackay GBInsider, University mod

    @markleavesley

    There are some efforts quietly going on in the background I believe.....seen mentions here and there but it's all kept under wraps.

  • markleavesleymarkleavesley USMember ✭✭

    Right. OK. Thanks. Guess we'll do it without Prism then!

  • NMackayNMackay GBInsider, University mod
    edited May 2017

    Well Prism 6.3 supports WPF but nothing officially targets all platforms.

    The navigation support for XF for example is radically different to WPF but Prism supports UWP & WPF but you can't target all these platforms in one solution, I don't know of an application framework (that's not very basic) that does.

    That's no comment on the work above but what I mean is you can't have a fully featured MVVM framework that targets all these technologies for now, if I'm wrong I'm more than happy to be corrected.

  • BrianLagunasBrianLagunas USInsider ✭✭✭

    You can easily share code across WPF and Xamarin.Forms with Prism. You just have to architect your app to plan fo rit. Here is a sample that should get you going in the right direction:

    https://github.com/brianlagunas/TodoApp

    Keep in mind, that WPF is a completely different beast than mobile platforms, so navigation is COMPLETELY different. Luckily Prism is the most robust WPF application framework out there.

  • NMackayNMackay GBInsider, University mod
    edited May 2017

    " is radically different to Prism" I meant....."WPF" hence the edit.

  • markleavesleymarkleavesley USMember ✭✭

    I don't think we'd have a problem with a different UI and navigation for WPF as it is classic desktop and everything else is a different generation of platform.

    Thanks for the replies on such an old topic.

  • markleavesleymarkleavesley USMember ✭✭

    @BrianLagunas - just tried the todo app. I don't really know how it works but one app on so many platforms. I've gone weak at the knees. Thank you.

  • juapepajuapepa ESMember

    @BrianLagunas

    First of all, we want to thank you for your dedication to improve the PRISM framework during all these years.

    We are a company that has been using PRISM in multiple WPF projects for many years.

    Now, we want to develop a new multiplatform application for mobile, tablet, desktop, etc. Following your TODO app (https://github.com/brianlagunas/TodoApp), we have started by creating a solution for each device. For example, the desktop solution is created with WPF and the mobile solution with Xamarin.Forms. We can reuse most of the code by sharing the view models which are .Net Standard libraries that use PRISM.Core.

    However, Microsoft recently announced that Xamarin.Forms will support WPF in addition to Linux and MacOS.

    Before this, we understood that Xamarin.Forms was only for mobile applications. However, after this announcement, Xamarin.Forms will also start to be used as a framework for desktop applications.

    That is the reason why we are thinking to work with Xamarin.Forms as a main development framework for all our applications, both for mobile and desktop devices. We could have different solutions for each device, but all of them would work with Xamarin.Forms with all the advantages that it represents (only one xaml to learn, only one framework, etc.).

    The problem is that PRISM for Xamarin.Forms does not have the concept of region. For us, in tablet or desktop solutions, this feature is essential to be able to create modular applications.

    In this post (http://brianlagunas.com/first-look-at-the-prism-for-xamarin-forms-preview/#comment-17787), you explained that regions didn't make sense in Xamarin.Forms for mobile devices. But now, after this announcement, it makes sense for larger devices like a tablet or desktop.

    Is there any plan to include the concept of regions in Xamarin.Forms? We think that both the community and us will be very interested.

    Thank you in advance

  • BrianLagunasBrianLagunas USInsider ✭✭✭

    @juapepa I am not sure about adding region support to XF or not. Honestly, I personally would not use XF to create an app for WPF. If your app is simple and needs very basic linear frame-based navigation, and you want it to be the exact same app on all devices, then this might be an option for you. The WPF apps I build are much more complicated than that. Then again, Xamarin might surprise me and make it extremely flexible.

    I have a number of other priorities I am working on at the moment, but I suggest submitting a request for regions in XF on the GitHub site. We can gauge community interest and have a more thoughtful conversation there. If regions were added, it would be a completely different API that what exists in WPF now, and most like much more limited due to the nature of XF.

  • juapepajuapepa ESMember

    @BrianLagunas

    Thank you for your recommendations.

    I have sent the following request: https://github.com/PrismLibrary/Prism/issues/1072.

    Kind Regards

  • bdkern01bdkern01 Member

    post might be a little bit old, but thanks, worked as advertised.

  • JBeckJBeck USUniversity ✭✭✭

    @bdkern01 Glad to hear it was useful to you, thanks for the feedback!

Sign In or Register to comment.