In: Computer Science
How can I configure the button in this tip calculator to calculate the total using the entires for the bill and tip percentage? I'm using Visual Studio 2019 Xamarin.Forms
Main.Page.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"
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
x:Class="Accomplish_2.MainPage"
BackgroundColor="Gray"
Padding="5">
<StackLayout>
<!-- Place new controls here -->
<Label Text="Tip Calculator"
HorizontalOptions="Center"
VerticalOptions="Center"
FontSize="Title"
FontAttributes="Bold"/>
<BoxView BackgroundColor="LightPink"
HeightRequest="3"></BoxView>
<Entry Placeholder="Bill Total"
Keyboard="Numeric"
x:Name="billTotal"></Entry>
<Entry Placeholder="Tip Percentage"
Keyboard="Numeric"
x:Name="tipPercent"></Entry>
<Button Text="Calculate"
Clicked="Button_Clicked"></Button>
</StackLayout>
</ContentPage>
Everything above should be good to go, I just can't figure out how to get some actual calculations done with the other page of my code.
Main.Page.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 Accomplish_2
{
// Learn more about making custom code visible in the Xamarin.Forms
previewer
// by visiting https://aka.ms/xamarinforms-previewer
[DesignTimeVisible(false)]
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
}
private void Button_Clicked(object sender, EventArgs e)
{
????????
_______________________________________________?????????
}
}
}
Explanation & steps
Based on code given, to calculate total using the entries for the bill and tip percentage using Visual Studio 2019 Xamarin.Forms. There are various ways to achieve this. The simplest way is to directly using x:name="billTotal" as billTotal.Text to read the text from input. This is direct way without any object oriented concept. The direct solution is provided below.
Only add [ var totalAmount= Double.Parse(billTotal.Text) + Double.Parse(tipPercent.Text);]
private void Button_Clicked(object sender, EventArgs e)
{
var totalAmount= Double.Parse(billTotal.Text) +
Double.Parse(tipPercent.Text);
}
Further direction, if there is enterprise level mobile application development.Recently as industry enterprise best practice we can use model view binder or MVVM model view view model application architecture. This is bit complex but there is lot of sample cose in Github for Xamarin.forms. One can visit Channel9.msdn microsoft to go through free videos to learn and implement.
MVVM Architecture- Binding Context & Data Binding
Data binding is binding between view and view model. The
BindingContext in presentation (view) is an object instance of the
ViewModel.