My First Xamarin Form Interactive Application
Celsius / Fahrenheit Converter
Using Visual Studio 2019 and Pixel 2 Pie 9.0 - API 28 (Android 9.0 - API 28) to create a simple Celsius / Fahrenheit converter mobile application.
Enter the temperature and select [Convert to Celsius] if you're using Fahrenheit and [Convert to Fahrenheit] if you're using Celsius. Simple.
MainPage.xaml
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Temperature.MainPage">
<StackLayout>
<Grid Padding="20">
<Grid.RowDefinitions>
<RowDefinition Height="50" />
<RowDefinition Height="50" />
<RowDefinition Height="50" />
<RowDefinition Height="50" />
<RowDefinition Height="50" />
<RowDefinition Height="50" />
<RowDefinition Height="50" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Label x:Name="lblPrompt" Text="Enter Temperature (degrees):" Grid.Row="0" Grid.Column="0" FontSize="20"/>
<Entry x:Name="initialTemp" Grid.Row="1" Grid.Column="0" FontSize="30"/>
<Label x:Name="lblResult" Text="Converted Temperature:" Grid.Row="2" Grid.Column="0" FontSize="20"/>
<Label x:Name="convertedTemp" Grid.Row="3" Grid.Column="0" FontSize="30"/>
<Button x:Name="BtnToCelsius" Text="Convert to Celsius" Grid.Row="4" Grid.Column="0" FontSize="20" Clicked="BtnToCelsius_Clicked" />
<Button x:Name="BtnToFahrenheit" Text="Convert to Fahrenheit" Grid.Row="5" Grid.Column="0" FontSize="20" Clicked="BtnToFahrenheit_Clicked" />
<Button x:Name="BtnReset" Text="Reset" Grid.Row="6" Grid.Column="0" FontSize="20" Clicked="BtnReset_Clicked" />
</Grid>
</StackLayout>
</ContentPage>
MainPage.xaml.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
namespace Temperature
{
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
}
//parse user input of degrees into a double
//public double initTemp;
private void BtnToCelsius_Clicked(object sender, EventArgs e)
{
try
{
//parse user input of degrees into a double
double initTemp = Double.Parse(initialTemp.Text);
//use formula of converting fahrenheit to celsius
double TempToCelsius = (initTemp - 32) * 5 / 9;
//output - convert to string
convertedTemp.Text = TempToCelsius.ToString() + " °C";
//disable both conversion buttons to avoid more than one conversion at a time
disableControls();
}
catch (Exception)
{
//if user does not enter a number, this error message will be displayed
convertedTemp.Text = "Please enter valid input.";
}
}
private void BtnToFahrenheit_Clicked(object sender, EventArgs e)
{
try
{
//parse user input of degrees into a double
double initTemp = Double.Parse(initialTemp.Text);
//use formula of converting celsius to fahrenheit
double TempToFahrenheit = (initTemp * 1.8) + 32;
//output - convert to string
convertedTemp.Text = TempToFahrenheit.ToString() + " °F";
//disable both conversion buttons to avoid more than one conversion at a time
disableControls();
}
catch (Exception)
{
//if user does not enter a number, this error message will be displayed
convertedTemp.Text = "Please enter valid input.";
}
}
//reset fields and enable buttons
private void BtnReset_Clicked(object sender, EventArgs e)
{
initialTemp.Text = String.Empty;
convertedTemp.Text = String.Empty;
enableControls();
}
//method that disables interactive controls
private void disableControls()
{
initialTemp.IsEnabled = false;
BtnToCelsius.IsEnabled = false;
BtnToFahrenheit.IsEnabled = false;
}
//method that enables interactive controls
private void enableControls()
{
initialTemp.IsEnabled = true;
BtnToCelsius.IsEnabled = true;
BtnToFahrenheit.IsEnabled = true;
}
}
}