Xamarin
Xamarin
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="ElectNext.MainPage">
<StackLayout>
<StackLayout>
<StackLayout HorizontalOptions="Center" VerticalOptions="Start">
<StackLayout HorizontalOptions="CenterAndExpand"
Orientation="Horizontal">
<Button x:Name="btnAdd" WidthRequest="200" Text="Add"
Clicked="BtnAdd_Clicked" />
<Button x:Name="btnRead" WidthRequest="200" Text="Read"
Clicked="BtnRead_Clicked" />
</StackLayout>
<StackLayout HorizontalOptions="CenterAndExpand"
Orientation="Horizontal">
<Button x:Name="btnUpdate" WidthRequest="200" Text="Update"
Clicked="BtnUpdate_Clicked"/>
<Button x:Name="btnDelete" WidthRequest="200" Text="Delete"
Clicked="BtnDelete_Clicked" />
</StackLayout>
<ListView x:Name="sPersons">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<ViewCell.View>
<Grid Padding="5">
<Grid.RowDefinitions>
<RowDefinition Height="60"></RowDefinition>
<RowDefinition Height="60"></RowDefinition>
<RowDefinition Height="10"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition
Width="1*"></ColumnDefinition>
<ColumnDefinition
Width="2*"></ColumnDefinition>
<ColumnDefinition
Width="3*"></ColumnDefinition>
</Grid.ColumnDefinitions>
</StackLayout>
</StackLayout>
</StackLayout>
</ContentPage>
MAIN.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 ElectNext
InitializeComponent();
base.OnAppearing();
if (personList != null)
try
// Validate input
if (string.IsNullOrEmpty(txtPresent.Text) ||
string.IsNullOrEmpty(txtPrevious.Text) ||
return;
// Parse input
return;
// Calculate ConsumptionReading
// Calculate electricityCharge
else
if (chkH.IsChecked)
demandCharge = 300;
serviceCharge = 200;
if (chkB.IsChecked)
demandCharge = 600;
serviceCharge = 400;
}
// Calculate PrincipalAmount
PresentReading = presentReading,
PreviousReading = previousReading,
ConsumptionReading = consumptionReading,
ElectricityCharge = electricityCharge,
DemandCharge = demandCharge,
ServiceCharge = serviceCharge,
PrincipalAmount = principalAmount,
Vat = vat,
AmountPayable = amountPayable
};
personsList.Add(person);
// Save personsList to SQLite database
await App.SQLiteDb.SaveItemsAsync(personsList);
// Update ListView
txtPresent.Text = "";
txtPrevious.Text = "";
chkH.IsChecked = false;
chkB.IsChecked = false;
if (!string.IsNullOrEmpty(txtMeterNum.Text))
// Get Person
if (person != null)
{
txtMeterNum.Text = "";
txtPresent.Text = "";
txtPrevious.Text = "";
else
await DisplayAlert("Not Found", "No person found with the given Meter No.", "OK");
else
try
if (!string.IsNullOrEmpty(txtMeterNum.Text))
{
// Fetch the person from the database using Meter No
if (person != null)
person.PresentReading = presentReading;
else
return;
if (chkH.IsChecked)
person.DemandCharge = 300;
person.ServiceCharge = 200;
else if (chkB.IsChecked)
person.DemandCharge = 600;
person.ServiceCharge = 400;
else
{
person.DemandCharge = 0;
person.ServiceCharge = 0;
// Calculate ConsumptionReading
else
{
// Calculate PrincipalAmount
await App.SQLiteDb.SaveItemAsync(person);
txtMeterNum.Text = "";
txtPresent.Text = "";
txtPrevious.Text = "";
else
else
if (!string.IsNullOrEmpty(txtMeterNum.Text))
if (person != null)
// Delete Person
await App.SQLiteDb.DeleteItemAsync(person);
txtMeterNum.Text = string.Empty;
RefreshPersonsList();
txtMeterNum.Text = "";
else
{
else
if (personList != null)
sPersons.ItemsSource = personList;
PERSON.CS
using SQLite;
using System;
using System.Collections.Generic;
using System.Text;
namespace ElectNext
{
public class Person
{
[PrimaryKey, AutoIncrement]
public int MeterNo { get; set; }
public double PresentReading { get; set; }
public double PreviousReading { get; set; }
public String TypeOfRegistration { get; set; }
public double ConsumptionReading { get; set; }
public double ElectricityCharge { get; set; }
public double DemandCharge { get; set; }
public double ServiceCharge { get; set; }
public double PrincipalAmount { get; set; }
public double Vat { get; set; }
public double AmountPayable { get; set; }
SQLiteHelper.CS
using SQLite;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace ElectNext
{
public class SQLiteHelper
{
SQLiteAsyncConnection db;
// Read Item
public Task<Person> GetItemAsync(int personId)
{
return db.Table<Person>().Where(i => i.MeterNo ==
personId).FirstOrDefaultAsync();
}
}
}
NEWPAGE.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 ElectNext
{