Working With Arithmetic Operators In Android And UWP Using Xamarin.Forms

Here, we will learn the process of working with arithmetic operators like +,-,,*,/ in Android and UWP using Xamarin.Forms.

Introduction

 
Xamarin is a platform that allows us to create a multi-platform app for Android, Windows, and iOS through a single integrated development environment (IDE) with Xamarin.Form. The interface design for all three platforms can be accomplished within its XAML-based standard, native user-interfaces control.
 
Let's start.
 
Step 1 
 
Open Visual Studio and go to New Project >> Installed>> Visual C# >> Cross-Platform. 
 
Picker Working Arithmetic Operators Android UWP Using Xamarin.Forms
 
Select the Cross- Platform app, then give your project a name and location.
 
Step 2 
 
Open Solution Eplorerer >> Project Name (Portable) >> App.xaml.cs >> double-click will open the design view of this page.
 
Picker Working Arithmetic Operators Android UWP Using Xamarin.Forms
 
The code is given below. 
 
Picker Working Arithmetic Operators Android UWP Using Xamarin.Forms 
 
C# Code 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. using Xamarin.Forms;  
  7.   
  8. namespace Picker  
  9. {  
  10.     public partial class App : Application  
  11.     {  
  12.         public App()  
  13.         {  
  14.             InitializeComponent();  
  15.   
  16.             MainPage = new NavigationPage (new MainPage());  
  17.         }  
  18.   
  19.         protected override void OnStart()  
  20.         {  
  21.             // Handle when your app starts  
  22.         }  
  23.   
  24.         protected override void OnSleep()  
  25.         {  
  26.             // Handle when your app sleeps  
  27.         }  
  28.   
  29.         protected override void OnResume()  
  30.         {  
  31.             // Handle when your app resumes  
  32.         }  
  33.     }  
  34. }  
Step 3 
 
Next, open Solution Explorer >> Project Name (Portable) >>Right-Click >> Add >> New Item or Ctrl+Shift+A.
 
Picker Working Arithmetic Operators Android UWP Using Xamarin.Forms
 
A new dialogue box will open. Now, add the XAML Page and give it a name. Click the "Add" button.
 
Picker Working Arithmetic Operators Android UWP Using Xamarin.Forms 
 
Step 4 
 
Open Solution Explorer >> Project Name >> MainPage.xaml. Open the design view of this page.
 
Picker Working Arithmetic Operators Android UWP Using Xamarin.Forms
 
The code is given below.
 
Picker Working Arithmetic Operators Android UWP Using Xamarin.Forms 
 
XAML Code
  1. <?xml version="1.0" encoding="utf-8" ?>    
  2. <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"    
  3.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"    
  4.              xmlns:local="clr-namespace:Picker"    
  5.              x:Class="Picker.MainPage">    
  6.     <ContentPage.Content>    
  7.         <StackLayout>    
  8.             <Button Text="Picker" Clicked="Button_Clicked"/>    
  9.         </StackLayout>    
  10.     </ContentPage.Content>    
  11. </ContentPage>    
Step 5 
 
Open Solution Explorer >> Project Name >> MainPage.xaml.cs. Open the design view of this page.
 
Picker Working Arithmetic Operators Android UWP Using Xamarin.Forms
 
The code is given below.
 
Picker Working Arithmetic Operators Android UWP Using Xamarin.Forms 
 
C# Code
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. using Xamarin.Forms;  
  7.   
  8. namespace Picker  
  9. {  
  10.     public partial class MainPage : ContentPage  
  11.     {  
  12.         public MainPage()  
  13.         {  
  14.             InitializeComponent();  
  15.         }  
  16.   
  17.          async private void Button_Clicked(object sender, EventArgs e)  
  18.         {  
  19.             await Navigation.PushAsync(new Page1());  
  20.         }  
  21.     }  
  22. }  
Step 6 
 
Open Solution Explorer >> Project Name >> Page1.xaml. Open the design view of this page.
 
Picker Working Arithmetic Operators Android UWP Using Xamarin.Forms
 
The code is given below.
 
Picker Working Arithmetic Operators Android UWP Using Xamarin.Forms
 
XAML Code 
  1. <?xml version="1.0" encoding="utf-8" ?>    
  2. <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"    
  3.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"    
  4.              x:Class="Picker.Page1">    
  5.     <ContentPage.Content>    
  6.         <StackLayout Orientation="Vertical">    
  7.             <Entry Placeholder="Enter Number 1" x:Name="No1"/>    
  8.             <Entry Placeholder="Enter Number 2" x:Name="No2"/>    
  9.             <Label Text="Select Operation" FontSize="Medium"/>    
  10.             <Picker x:Name="Pk" SelectedIndexChanged="Pk_SelectedIndexChanged"/>    
  11.             <Label Text="------" x:Name="mylab"/>    
  12.         </StackLayout>    
  13.     </ContentPage.Content>    
  14. </ContentPage>    
Step 7
 
Open Solution Explorer >> Project Name >> Page.xaml.cs. Open the design view of this page.
 
Picker Working Arithmetic Operators Android UWP Using Xamarin.Forms
 
The code is given below.
 
Picker Working Arithmetic Operators Android UWP Using Xamarin.Forms
 
C# Code
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6.   
  7. using Xamarin.Forms;  
  8. using Xamarin.Forms.Xaml;  
  9.   
  10. namespace Picker  
  11. {  
  12.     [XamlCompilation(XamlCompilationOptions.Compile)]  
  13.     public partial class Page1 : ContentPage  
  14.     {  
  15.         public Page1()  
  16.         {  
  17.             InitializeComponent();  
  18.             Pk.Items.Add("Addition");  
  19.             Pk.Items.Add("Subtraction");  
  20.             Pk.Items.Add("Multiplication");  
  21.             Pk.Items.Add("Divition");  
  22.         }  
  23.   
  24.         private void Pk_SelectedIndexChanged(object sender, EventArgs e)  
  25.         {  
  26.             string op = Pk.SelectedItem.ToString();  
  27.             int n1 = Int32.Parse(No1.Text);  
  28.             int n2 = Int32.Parse(No2.Text);  
  29.             double res = 0;  
  30.   
  31.             if (op == "Addition")  
  32.                 res = n1 + n2;  
  33.             else if (op == "Subtraction")  
  34.                 res = n1 - n2;  
  35.             else if (op == "Multiplication")  
  36.                 res = n1 * n2;  
  37.             else if (op == "Divition")  
  38.                 res = n1 / n2;  
  39.   
  40.             mylab.Text = res + "";  
  41.         }  
  42.     }  
  43. }  
Step 8 
 
Next, select the "Build and Deploy" option followed by selecting from the list Android emulator and simulator. you can choose any API (Application Program Interface) level emulator or simulator to run it.
 
Output 
 
After a few seconds, you will see your app working.
 
Android Output 
 
You can choose the Android platform.

Picker Working Arithmetic Operators Android UWP Using Xamarin.Forms

 

Enter the number and select the operation.
 
Picker Working Arithmetic Operators Android UWP Using Xamarin.Forms
 
Click Addition Operation.
 
Picker Working Arithmetic Operators Android UWP Using Xamarin.Forms
 
The result is displayed below.
 
Picker Working Arithmetic Operators Android UWP Using Xamarin.Forms
 
Click "Subtraction" operation.
 
Picker Working Arithmetic Operators Android UWP Using Xamarin.Forms
 
The result is displayed below.
 
Picker Working Arithmetic Operators Android UWP Using Xamarin.Forms
 
Click the "Multiplication" operation.
 
Picker Working Arithmetic Operators Android UWP Using Xamarin.Forms
 
The result is displayed below.
 
Picker Working Arithmetic Operators Android UWP Using Xamarin.Forms
 
Click the "Division" operation.
 
Picker Working Arithmetic Operators Android UWP Using Xamarin.Forms
 
The result is displayed below.
 
Picker Working Arithmetic Operators Android UWP Using Xamarin.Forms
 
Windows Output 
 
Let us now choose the UWP Platform.

Picker Working Arithmetic Operators Android UWP Using Xamarin.Forms

 

Enter the number and select the Addition operator. Show the picker and choose.
 
Picker Working Arithmetic Operators Android UWP Using Xamarin.Forms
 
Choose the Addition operation. The result is displayed below.
 
Picker Working Arithmetic Operators Android UWP Using Xamarin.Forms
 
Choose the Subtraction operation. The result is displayed below.
 
Picker Working Arithmetic Operators Android UWP Using Xamarin.Forms 
 
Choose the Multiplication operator. The result is displayed below.
 
Picker Working Arithmetic Operators Android UWP Using Xamarin.Forms
 
Choose the Division operation. The result is displayed below. 
 
Picker Working Arithmetic Operators Android UWP Using Xamarin.Forms
 

Conclusion

 
I hope you have learned about Arithmetic Operations in Android and UWP using Xamarin.Forms with Visual Studio and C#.