Hey guys this is asimple implementation of calculator in Xamarin Forms Cross Platform
Enjoy the code: :D
Step1: Your MainPage.xaml will look like this
Step2: Your MainPage.xaml.cs will look like this
Step3: Your OperatorHelper.cs will lookLike this
Step4: You can change Android MainActivity.cs to get round buttons like this:(Not Recommended to implement)
Enjoy the code: :D
Step1: Your MainPage.xaml will look like this
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8" ?> | |
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" | |
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" | |
xmlns:local="clr-namespace:XamarinCalculator" | |
x:Class="XamarinCalculator.MainPage"> | |
<ContentPage.Padding> | |
<OnPlatform x:TypeArguments="Thickness"> | |
<OnPlatform.iOS>0,20,0,0</OnPlatform.iOS> | |
</OnPlatform> | |
</ContentPage.Padding> | |
<ContentPage.Resources> | |
<ResourceDictionary> | |
<Style x:Key ="infostyle" TargetType="Button"> | |
<Setter Property="WidthRequest" Value="60" /> | |
<Setter Property="HeightRequest" Value="60"/> | |
<Setter Property="BorderRadius" Value="30"/> | |
<Setter Property="TextColor" Value="White"/> | |
<Setter Property="FontSize" Value="36"/> | |
</Style> | |
</ResourceDictionary> | |
</ContentPage.Resources> | |
<Grid Padding="5,0" RowSpacing="5" ColumnSpacing="5" > | |
<Grid.RowDefinitions> | |
<RowDefinition Height="2*" /> | |
<RowDefinition Height="*" /> | |
<RowDefinition Height="*" /> | |
<RowDefinition Height="*" /> | |
<RowDefinition Height="*" /> | |
<RowDefinition Height="*" /> | |
</Grid.RowDefinitions> | |
<Grid.ColumnDefinitions> | |
<ColumnDefinition Width="*" /> | |
<ColumnDefinition Width="*" /> | |
<ColumnDefinition Width="*" /> | |
<ColumnDefinition Width="*" /> | |
</Grid.ColumnDefinitions> | |
<Label x:Name="resultText" FontSize="48" FontAttributes="Bold" BackgroundColor="Black" Text="0" | |
TextColor="White" HorizontalTextAlignment="End" VerticalTextAlignment="Center" | |
LineBreakMode="NoWrap" Grid.ColumnSpan="4" /> | |
<Button Text="AC" Grid.Row="1" Grid.Column="0" Style="{StaticResource infostyle}" BackgroundColor="#808080" Clicked="OnClear" /> | |
<Button Text="√" Grid.Row="1" Grid.Column="1" Style="{StaticResource infostyle}" BackgroundColor="#808080" FontSize="36" Clicked="OnSquareRoot"/> | |
<Button BorderRadius="30" Text="%" Grid.Row="1" Grid.Column="2" Style="{StaticResource infostyle}" BackgroundColor="#808080" Clicked="OnPercentage"/> | |
<Button Text="7" Grid.Row="2" Grid.Column="0" BackgroundColor="#2e2e2e" Clicked="OnSelectNumber" Style="{StaticResource infostyle}"/> | |
<Button Text="8" Grid.Row="2" Grid.Column="1" BackgroundColor="#2e2e2e" Clicked="OnSelectNumber" Style="{StaticResource infostyle}" /> | |
<Button Text="9" Grid.Row="2" Grid.Column="2" BackgroundColor="#2e2e2e" Clicked="OnSelectNumber" Style="{StaticResource infostyle}" /> | |
<Button Text="4" Grid.Row="3" Grid.Column="0" BackgroundColor="#2e2e2e" Clicked="OnSelectNumber" Style="{StaticResource infostyle}"/> | |
<Button Text="5" Grid.Row="3" Grid.Column="1" BackgroundColor="#2e2e2e" Clicked="OnSelectNumber" Style="{StaticResource infostyle}" /> | |
<Button Text="6" Grid.Row="3" Grid.Column="2" BackgroundColor="#2e2e2e" Clicked="OnSelectNumber" Style="{StaticResource infostyle}" /> | |
<Button Text="1" Grid.Row="4" Grid.Column="0" BackgroundColor="#2e2e2e" Clicked="OnSelectNumber" Style="{StaticResource infostyle}"/> | |
<Button Text="2" Grid.Row="4" Grid.Column="1" BackgroundColor="#2e2e2e" Clicked="OnSelectNumber" Style="{StaticResource infostyle}"/> | |
<Button Text="3" Grid.Row="4" Grid.Column="2" BackgroundColor="#2e2e2e" Clicked="OnSelectNumber" Style="{StaticResource infostyle}" /> | |
<Button Text="0" Grid.Row="5" Grid.Column="0" Grid.ColumnSpan="2" BackgroundColor="#2e2e2e" Clicked="OnSelectNumber" Style="{StaticResource infostyle}"/> | |
<Button Text="x2" Grid.Row="5" Grid.Column="2" BackgroundColor="#2e2e2e" Clicked="squareclicked" Style="{StaticResource infostyle}" /> | |
<Button Text="=" Grid.Row="5" Grid.Column="3" BackgroundColor="#EE7600" Clicked="OnCalculate" Style="{StaticResource infostyle}"/> | |
<Button Text="÷" Grid.Row="1" Grid.Column="3" BackgroundColor="#EE7600" Clicked="OnSelectOperator" Style="{StaticResource infostyle}"/> | |
<Button Text="*" Grid.Row="2" Grid.Column="3" BackgroundColor="#EE7600" Clicked="OnSelectOperator" Style="{StaticResource infostyle}" /> | |
<Button Text="-" Grid.Row="3" Grid.Column="3" BackgroundColor="#EE7600" Clicked="OnSelectOperator" Style="{StaticResource infostyle}" /> | |
<Button Text="+" Grid.Row="4" Grid.Column="3" BackgroundColor="#EE7600" Clicked="OnSelectOperator" Style="{StaticResource infostyle}" /> | |
</Grid> | |
</ContentPage> |
Step2: Your MainPage.xaml.cs will look like this
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using Xamarin.Forms; | |
namespace XamarinCalculator | |
{ | |
public partial class MainPage : ContentPage | |
{ | |
int currentState = 1; | |
string myoperator; | |
double firstNumber, secondNumber; | |
public MainPage() | |
{ | |
InitializeComponent(); | |
OnClear(this, null); | |
} | |
void OnSelectNumber(object sender, EventArgs e) | |
{ | |
Button button = (Button)sender; | |
string pressed = button.Text; | |
//validation before button is pressed | |
//if the current result is 0 in text box then we will direct the calculator to exclude 0 when pressing busttons | |
if (this.resultText.Text == "0" || currentState < 0 )//at first current state is 1 | |
{ | |
this.resultText.Text = "";//here the text value will be cleared when pressing button | |
if (currentState < 0) //at first current value is 1 so this condition is excluded | |
currentState *= -1; | |
} | |
this.resultText.Text += pressed;// this condition is called when current state is greater and text box will aquire the pressed | |
double number;//if we are going to assign two dynamic number for a given variable using try parse method | |
if (double.TryParse(this.resultText.Text, out number)) | |
{ | |
this.resultText.Text = number.ToString("N0"); | |
if (currentState == 1) | |
{ | |
firstNumber = number;//at first current state will be 1 and it will assign first number with the pressed number variable | |
} | |
else | |
{ | |
secondNumber = number;//it will be implemented as the number of current state changes i.e. 2 | |
} | |
} | |
} | |
void OnSelectOperator(object sender, EventArgs e)//event is called when the select operator is called | |
{ | |
currentState = -2; | |
Button button = (Button)sender; | |
string pressed = button.Text; | |
myoperator = pressed; | |
} | |
void OnClear(object sender, EventArgs e)// this method is called when we press the AC button | |
{ | |
firstNumber = 0; | |
secondNumber = 0; | |
currentState = 1; | |
this.resultText.Text = "0"; | |
} | |
void OnPercentage(object sender,EventArgs e) //This method is useful when we are going to find out percentage of last rsulting variable or the initial variable | |
{ | |
if ((currentState == -1) || (currentState == 1))//please use some break point to check when we are going to get current state value as -1 or 1 | |
{ | |
//var result = OperatorHelper.MyPercentage(firstNumber, myoperator); | |
var result = firstNumber / 100; | |
this.resultText.Text = result.ToString(); | |
firstNumber = result; | |
currentState = -1; | |
} | |
} | |
void OnCalculate(object sender, EventArgs e) //This method is called when we have both first number and second number and we are going to evaluate those number | |
{ | |
if (currentState == 2) | |
{ | |
var result = OperatorHelper.Calculate(firstNumber, secondNumber, myoperator); | |
this.resultText.Text = result.ToString(); | |
firstNumber = result; | |
currentState = -1; | |
} | |
} | |
void OnSquareRoot(object sender, EventArgs e) //We call this event when we have one resulting number or initial number ,we are going to find out the square root of that number | |
{ | |
if ((currentState == -1)||(currentState == 1)) | |
{ | |
//var result = OperatorHelper.MySquareRoot(firstNumber, myoperator); | |
var result = Math.Sqrt(firstNumber); | |
this.resultText.Text = result.ToString(); | |
firstNumber = result; | |
currentState = -1; | |
} | |
} | |
private void squareclicked(object sender, EventArgs e)//We call this method when we have one resulting number or initial number ,we are going to find out the square root of that number | |
{ | |
if ((currentState == -1) || (currentState == 1)) | |
{ | |
//var result = OperatorHelper.MySquare(firstNumber, myoperator); | |
var result = firstNumber * firstNumber; | |
this.resultText.Text = result.ToString(); | |
firstNumber = result; | |
currentState = -1; | |
} | |
} | |
} | |
} | |
Step3: Your OperatorHelper.cs will lookLike this
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Text; | |
namespace XamarinCalculator | |
{ | |
public static class OperatorHelper | |
{ | |
public static double Calculate(double value1 , double value2 , string myoperator) | |
{ | |
double result = 0; | |
switch(myoperator) | |
{ | |
case "÷": | |
result = value1 / value2; | |
break; | |
case "*": | |
result = value1 * value2; | |
break; | |
case "+": | |
result = value1 + value2; | |
break; | |
case "-": | |
result = value1 - value2; | |
break; | |
} | |
return result; | |
} | |
} | |
} | |
Step4: You can change Android MainActivity.cs to get round buttons like this:(Not Recommended to implement)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using Android.App; | |
using Android.Content.PM; | |
using Android.Runtime; | |
using Android.Views; | |
using Android.Widget; | |
using Android.OS; | |
namespace XamarinCalculator.Droid | |
{ | |
[Activity(Label = "XamarinCalculator", Icon = "@drawable/icon", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)] | |
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsApplicationActivity | |
{ | |
protected override void OnCreate(Bundle bundle) | |
{ | |
//TabLayoutResource = Resource.Layout.Tabbar; | |
//ToolbarResource = Resource.Layout.Toolbar; | |
base.OnCreate(bundle); | |
global::Xamarin.Forms.Forms.Init(this, bundle); | |
LoadApplication(new App()); | |
} | |
} | |
} | |
bet365 Link | Daftar Slot Online Gacor 2021 – Activity
ReplyDeleteThe betting company is a top operator in the Asian dafabet link market. 제왕카지노 To reach bet365, you must have registered with bet365 in order to 바카라사이트