You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
MyFirstMAUI/MyFirstMauiApp/SharedResources/CustomTipPage.xaml.cs

56 lines
1.4 KiB

namespace SharedResources;
public partial class CustomTipPage : ContentPage
{
public CustomTipPage()
{
InitializeComponent();
billInput.TextChanged += (s, e) => CalculateTip(false, false);
roundDown.Clicked += (s, e) => CalculateTip(false, true);
roundUp.Clicked += (s, e) => CalculateTip(true, false);
tipPercentSlider.ValueChanged += (s, e) =>
{
double pct = Math.Round(e.NewValue);
tipPercent.Text = $"{pct}%";
CalculateTip(false, false);
};
}
void CalculateTip(bool roundUp, bool roundDown)
{
double t;
if (Double.TryParse(billInput.Text, out t) && t > 0)
{
double pct = Math.Round(tipPercentSlider.Value);
double tip = Math.Round(t * (pct / 100.0), 2);
double final = t + tip;
if (roundUp)
{
final = Math.Ceiling(final);
tip = final - t;
}
else if (roundDown)
{
final = Math.Floor(final);
tip = final - t;
}
tipOutput.Text = tip.ToString("C");
totalOutput.Text = final.ToString("C");
}
}
private void OnNormalTip(object sender, EventArgs e)
{
tipPercentSlider.Value = 15;
}
private void OnGenerousTip(object sender, EventArgs e)
{
tipPercentSlider.Value = 20;
}
}