Prism In Xamarin.Forms Application For Android And UWP

Prism is a framework for building loosely coupled, maintainable, and testable XAML applications in WPF, Windows 10 UWP, and Xamarin Forms. Prism provides an implementation of a collection of design patterns that are helpful in writing well-structured and maintainable XAML applications, including MVVM, dependency injection, commands, Event Aggregator, and others. Prism's core functionality is a shared code base in a Portable Class Library targeting these platforms.

More information about Prism.

Reading this article, you will learn how to use Prism in Xamarin Forms application for Android and Universal Windows Platform with XAML and Visual C# in cross-platform application development.

The following important tools are required for developing UWP.

  1. Windows 10 (Recommended)
  2. Visual Studio 2017 Community/Enterprise/Professional Edition (Software available online https://www.visualstudio.com/downloads/)
  3. Using Visual studio 2017 Installer, Enable the feature of Mobile development with .NET.

Now, let's discuss step by step App development.

Step 1

Adding Prisim template pack to your project.

Open Visual Studio 2017 -> select Tools Menu -> select Extension and Updates.

Select Online and search Prism.
Select Prism Template Pack -> install.



Step 2

Open Visual Studio 2017.
Go to Start -> New Project.
Select Prism (under Visual C#) ->select Xamarin.Forms -> select Prism Unity App (Xamarin.Forms).
Give a suitable name for your App (XamFormPrism) ->OK.



Step 3

Select the platform in Prism Project Wizard.



Step 4

Afterwards, Visual Studio creates 4 projects (Portable, Droid, iOS, UWP) and portable project having the folder of View Models and views in the concept of MVVM.



Step 5

In Views folder >> MainPage.Xaml Page, add labels for displaying a message.
  1. <StackLayout HorizontalOptions="Center" VerticalOptions="Center">  
  2.     <Label Text="Prism for Xamarin Forms application in Android and UWP" FontSize="20" VerticalOptions="Center" HorizontalOptions="Center" />  
  3.     <Label/>  
  4.     <Label/>  
  5.     <Label/>  
  6.     <Label Text="{Binding Message}" />   
  7. </StackLayout>  

Step 6

In ViewsModel Folder MainPageViewModel.cs Page, add the following code for binding a message.
  1. private string _message = "Welcome to Prism for Xamarin Forms application";  
  2. public string Message {  
  3.     get {  
  4.         return _message;  
  5.     }  
  6.     set {  
  7.         SetProperty(ref _message, value);  
  8.     }  
  9. }  

Step 7

We will test Android and UWP. So, we can set the Multiple Startup Projects as XamFormPrism.Droid and XamFormPrism.UWP (Universal Windows)

Step 8

Change the Configuration Manager settings.
 
 Go to Build -> Configuration Manager,

Uncheck the Build and Deploy options to the iOS and check the Droid and UWP.

Step 9

Deploy your app to Android Emulator and Local Machine (UWP) and the output of the XamFormPrism App is,

Summary

Now, you have successfully tested Prism in Xamarin.Forms application for Cross Platform Application Development using Visual C# and Xamarin.

Please find the link for Source Code.

Up Next
    Ebook Download
    View all
    Learn
    View all