Click here to Skip to main content
15,887,083 members
Articles / Mobile Apps / Xamarin

XF 3.0 New Feature: Create WPF Application using Xamarin.Forms

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
17 May 2018CPOL3 min read 9.4K   3   1
Creating WPF applications using Xamarin.Forms

Yes, you read the title right! In this blog, we’ll talk about creating WPF applications using Xamarin.Forms πŸ™‚ - a little background first. One of the biggest developer conferences organized by Microsoft happened in Seattle last week where Xamarin team released the stable version of Xamarin.Forms 3.0. One of the features this release has that it supports WPF windows desktop applications using Xamarin.Forms. In this blog, I will give you a step by step guide on how to add WPF application in your Xamarin.Forms application solution and then make the changes in it to use Xamarin.Forms controls in WPF.

As WPF works only on Windows, all our steps will be done in Visual Studio 2017 15.7 version on Windows.

Image 2

  1. Create is new Xamarin.Forms project by clicking on new project => cross platform Xamarin.Forms blank application using .NET standard, you will get the following project structure which will have a .NET standard project, iOS project, Android project and UWP project if you have your UWP SDK installed and are working on Windows 10.

    Image 3

  2. Now right click on the solution file and select the option β€œManage nuget packages for Solution” from popup menu. On manage nuget packages window, go to updates tab and update your Xamarin.Forms package to the latest version that is 3.0. In my case, since I have already updated, it’s shown in the installed tab.

    Image 4

  3. Close the Nuget Package Manager window, right click on the solution file again and select Add => New Project option from the popup menu, it will open the following window, where you have to click on Windows Desktop option in the left window and select WPF App (.NET Framework) like I have done. Give the name to the project and click Ok.

    Image 5

  4. Once the project is created, right click on WPF project file in Solution Explorer and select β€œManage nuget Packages” from popup menu. On β€œManage nuget Packages” window, click on Browse Tab, Install Xamarin.Forms nuget Package to the project. then select the include prerelease checkbox, browse for β€œXamarin.Forms.Platform.WPF” and click on install button as highlighted in the below screenshots:

    Image 6

    Image 7

  5. Once Xamarin.Forms.Platform.WPF is installed, close the Nuget Package Manager window, right click on References of WPF project and select Add Reference option from popup menu to add the reference of .NET Standard Project from the Projects => Solution option in the window as highlighted in the below screenshot:

    Image 8

  6. In MainWindow.xaml file of WPF application, change the root node from Window to wpf:FormsApplicationPage and add xmlns:wpf=”clr-namespace:Xamarin.Forms.Platform.WPF;assembly=Xamarin.Forms.Platform.WPF” in namespace declarations as shown in the below image:

    Image 9

  7. In MainWindow.xaml.cs file of WPF application, add the following two using directives
    C#
    using Xamarin.Forms;
    	using Xamarin.Forms.Platform.WPF;

    and change the parent class from Window to FormsApplicationPage and add following two lines of code in the constructor:

    C#
    Forms.Init();
    	LoadApplication(new TrXamGuide.App());
    These two lines will initialize Xamarin.Forms and load the Xamarin.Forms app created in .NET Standard project. Below is the screenshot highlighting the code differences:

    Image 10

  8. Your WPF application using Xamarin.Forms is now ready to execute. This is how it will look once executed.

Please keep in mind that Xamarin.Forms.Platform.WPF is still in pre-release so all the features of Xamarin.Forms will not be available in it, to get the complete list of available features, use this link

You can get the complete solution code from here. Let me know if I have missed anything or you want to know about anything in particular related to Xamarin.Forms development. I will be coming up with more such articles in the near future.

πŸ™‚ πŸ™‚ πŸ™‚ πŸ™‚ Happy coding!!! πŸ™‚ πŸ™‚ πŸ™‚ πŸ™‚

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
India India
Hi There, I am an IT professional with 14 years of experience in architecting, designing and building IT solutions for complex business needs in form of mobile & web applications using Microsoft technologies. Currently working in an multinational company in India as Solutions Architect. The articles here are sourced from my blog : http://techierathore.com/

Comments and Discussions

 
QuestionExecution on Mac. Pin
AchLog18-May-18 9:48
AchLog18-May-18 9:48 
Hello S Ravi,

I just read your article which is interesting and, perhaps, promising for my project.
I developed a desktop WPF application under .Net Framework and I would like to make it work on Mac.

One of its important features is that it displays images and processes mouse events to draw right segments on them.

In this context, is Xamarin.Forms a good candidate?

Thank you for your reply

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.