Added TipCalc Sample
This commit is contained in:
parent
750adae87c
commit
f1e203c325
|
@ -31,6 +31,7 @@ namespace Samples
|
|||
new FilesSample ().Publish ();
|
||||
new DisplayAlertSample ().Publish ();
|
||||
new EditorSample().Publish();
|
||||
new TipCalcSample().Publish();
|
||||
new XuzzleSample().Publish();
|
||||
|
||||
UI.Present ("/display-alert");
|
||||
|
|
|
@ -38,6 +38,9 @@
|
|||
<EmbeddedResource Update="DisplayAlertPage.xaml">
|
||||
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
|
||||
</EmbeddedResource>
|
||||
<EmbeddedResource Update="TipCalc\TipCalcPage.xaml">
|
||||
<Generator>MSBuild:Compile</Generator>
|
||||
</EmbeddedResource>
|
||||
<EmbeddedResource Update="XamlPreviewPage.xaml">
|
||||
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
|
||||
</EmbeddedResource>
|
||||
|
@ -46,10 +49,7 @@
|
|||
<ItemGroup>
|
||||
<None Remove="BugSweeper\Images\RedBug.png" />
|
||||
<None Remove="BugSweeper\Images\Xamarin120.png" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="TipCalc\" />
|
||||
<None Remove="TipCalc\TipCalcPage.xaml" />
|
||||
</ItemGroup>
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
|
|
|
@ -0,0 +1,33 @@
|
|||
using System;
|
||||
using System.Globalization;
|
||||
using Xamarin.Forms;
|
||||
|
||||
namespace TipCalc
|
||||
{
|
||||
public class DoubleRoundingConverter : IValueConverter
|
||||
{
|
||||
public object Convert(object value, Type targetType,
|
||||
object parameter, CultureInfo culture)
|
||||
{
|
||||
return Round((double)value, parameter);
|
||||
}
|
||||
|
||||
public object ConvertBack(object value, Type targetType,
|
||||
object parameter, CultureInfo culture)
|
||||
{
|
||||
return Round((double)value, parameter);
|
||||
}
|
||||
|
||||
double Round(double number, object parameter)
|
||||
{
|
||||
double precision = 1;
|
||||
|
||||
// Assume parameter is string encoding precision.
|
||||
if (parameter != null)
|
||||
{
|
||||
precision = Double.Parse((string)parameter);
|
||||
}
|
||||
return precision * Math.Round(number / precision);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,32 @@
|
|||
using System;
|
||||
using System.Globalization;
|
||||
using Xamarin.Forms;
|
||||
|
||||
namespace TipCalc
|
||||
{
|
||||
public class DoubleToStringConverter : IValueConverter
|
||||
{
|
||||
public object Convert(object value, Type targetType,
|
||||
object parameter, CultureInfo culture)
|
||||
{
|
||||
// Assumes value is double.
|
||||
double number = (double)value;
|
||||
|
||||
// Return empty string for a zero (good for Entry views).
|
||||
if (number == 0)
|
||||
{
|
||||
return "";
|
||||
}
|
||||
|
||||
return number.ToString();
|
||||
}
|
||||
|
||||
public object ConvertBack(object value, Type targetType,
|
||||
object parameter, CultureInfo culture)
|
||||
{
|
||||
double number = 0;
|
||||
Double.TryParse((string)value, out number);
|
||||
return number;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,111 @@
|
|||
using System;
|
||||
using System.ComponentModel;
|
||||
|
||||
namespace TipCalc
|
||||
{
|
||||
class TipCalcModel : INotifyPropertyChanged
|
||||
{
|
||||
double subTotal, postTaxTotal, tipPercent, tipAmount, total;
|
||||
|
||||
public event PropertyChangedEventHandler PropertyChanged;
|
||||
|
||||
public double SubTotal
|
||||
{
|
||||
set
|
||||
{
|
||||
if (subTotal != value)
|
||||
{
|
||||
subTotal = value;
|
||||
OnPropertyChanged("SubTotal");
|
||||
Recalculate();
|
||||
}
|
||||
}
|
||||
get
|
||||
{
|
||||
return subTotal;
|
||||
}
|
||||
}
|
||||
|
||||
public double PostTaxTotal
|
||||
{
|
||||
set
|
||||
{
|
||||
if (postTaxTotal != value)
|
||||
{
|
||||
postTaxTotal = value;
|
||||
OnPropertyChanged("PostTaxTotal");
|
||||
Recalculate();
|
||||
}
|
||||
}
|
||||
get
|
||||
{
|
||||
return postTaxTotal;
|
||||
}
|
||||
}
|
||||
|
||||
public double TipPercent
|
||||
{
|
||||
set
|
||||
{
|
||||
if (tipPercent != value)
|
||||
{
|
||||
tipPercent = value;
|
||||
OnPropertyChanged("TipPercent");
|
||||
Recalculate();
|
||||
}
|
||||
}
|
||||
get
|
||||
{
|
||||
return tipPercent;
|
||||
}
|
||||
}
|
||||
|
||||
public double TipAmount
|
||||
{
|
||||
set
|
||||
{
|
||||
if (tipAmount != value)
|
||||
{
|
||||
tipAmount = value;
|
||||
OnPropertyChanged("TipAmount");
|
||||
}
|
||||
}
|
||||
get
|
||||
{
|
||||
return tipAmount;
|
||||
}
|
||||
}
|
||||
|
||||
public double Total
|
||||
{
|
||||
set
|
||||
{
|
||||
if (total != value)
|
||||
{
|
||||
total = value;
|
||||
OnPropertyChanged("Total");
|
||||
}
|
||||
}
|
||||
get
|
||||
{
|
||||
return total;
|
||||
}
|
||||
}
|
||||
|
||||
void Recalculate()
|
||||
{
|
||||
this.TipAmount = Math.Round(this.TipPercent * this.SubTotal / 100, 2);
|
||||
|
||||
// Round total to nearest quarter.
|
||||
this.Total = Math.Round(4 * (this.PostTaxTotal + this.TipAmount)) / 4;
|
||||
}
|
||||
|
||||
protected void OnPropertyChanged(string propertyName)
|
||||
{
|
||||
if (PropertyChanged != null)
|
||||
{
|
||||
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,55 @@
|
|||
<?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:TipCalc"
|
||||
x:Class="TipCalc.TipCalcPage">
|
||||
<ContentPage.Padding>
|
||||
<OnPlatform x:TypeArguments="Thickness">
|
||||
<On Platform="iOS" Value="5, 20, 5, 0" />
|
||||
<On Platform="Android, UWP, WinRT, WinPhone" Value="5, 0, 5, 0" />
|
||||
</OnPlatform>
|
||||
</ContentPage.Padding>
|
||||
<ContentPage.Resources>
|
||||
<ResourceDictionary>
|
||||
<local:TipCalcModel x:Key="model" TipPercent="15" />
|
||||
<local:DoubleToStringConverter x:Key="stringConverter" />
|
||||
<local:DoubleRoundingConverter x:Key="roundConverter" />
|
||||
</ResourceDictionary>
|
||||
</ContentPage.Resources>
|
||||
<Grid BindingContext="{StaticResource model}">
|
||||
<Grid.RowDefinitions>
|
||||
<RowDefinition Height="Auto" />
|
||||
<RowDefinition Height="Auto" />
|
||||
<RowDefinition Height="Auto" />
|
||||
<RowDefinition Height="Auto" />
|
||||
<RowDefinition Height="Auto" />
|
||||
<RowDefinition Height="Auto" />
|
||||
</Grid.RowDefinitions>
|
||||
<Grid.ColumnDefinitions>
|
||||
<ColumnDefinition Width="*" />
|
||||
<ColumnDefinition Width="*" />
|
||||
</Grid.ColumnDefinitions>
|
||||
<!-- Row 0 -->
|
||||
<Label Text="Food & Drink:" Grid.Row="0" Grid.Column="0" Font="Large" HorizontalTextAlignment="End" VerticalTextAlignment="Center" />
|
||||
<Entry Grid.Row="0" Grid.Column="1" Keyboard="Numeric" Placeholder="Subtotal" Text="{Binding SubTotal, 
 Converter={StaticResource stringConverter}}" />
|
||||
<!-- Row 1 -->
|
||||
<Label Text="Total after Tax:" Grid.Row="1" Grid.Column="0" Font="Large" HorizontalTextAlignment="End" VerticalTextAlignment="Center" />
|
||||
<Entry Grid.Row="1" Grid.Column="1" Keyboard="Numeric" Placeholder="Receipt total" Text="{Binding PostTaxTotal,
 Converter={StaticResource stringConverter}}" />
|
||||
<!-- Row 2 -->
|
||||
<Label Text="Tip Percent:" Grid.Row="2" Grid.Column="0" Font="Large" HorizontalTextAlignment="End" VerticalTextAlignment="Center" />
|
||||
<Entry Grid.Row="2" Grid.Column="1" Keyboard="Numeric" Text="{Binding TipPercent,
 Converter={StaticResource stringConverter}}" />
|
||||
<!-- Row 3 -->
|
||||
<Slider Grid.Row="3" Grid.Column="0" Grid.ColumnSpan="2" Minimum="0" Maximum="100" Value="{Binding TipPercent,
 Mode=TwoWay,
 Converter={StaticResource roundConverter},
 ConverterParameter=0.5}" />
|
||||
<!-- Row 4 -->
|
||||
<Label Text="Tip Amount:" Grid.Row="4" Grid.Column="0" Font="Large" HorizontalTextAlignment="End" VerticalTextAlignment="Center" />
|
||||
<ContentView BackgroundColor="#40808080" Grid.Row="4" Grid.Column="1" Padding="10, 10, 40, 10">
|
||||
<Label Text="{Binding TipAmount,
 StringFormat='{0:C}'}" Font="Large" HorizontalTextAlignment="End" />
|
||||
</ContentView>
|
||||
<!-- Row 5 -->
|
||||
<Label Text="Total:" Grid.Row="5" Grid.Column="0" Font="Large" HorizontalTextAlignment="End" VerticalTextAlignment="Center" />
|
||||
<ContentView BackgroundColor="#40808080" Grid.Row="5" Grid.Column="1" Padding="10, 10, 40, 10">
|
||||
<Label Text="{Binding Total, 
 StringFormat='{0:C}'}" Font="Large" HorizontalTextAlignment="End" />
|
||||
</ContentView>
|
||||
</Grid>
|
||||
</ContentPage>
|
|
@ -0,0 +1,10 @@
|
|||
namespace TipCalc
|
||||
{
|
||||
public partial class TipCalcPage
|
||||
{
|
||||
public TipCalcPage()
|
||||
{
|
||||
InitializeComponent();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
using Ooui;
|
||||
using Xamarin.Forms;
|
||||
|
||||
namespace Samples
|
||||
{
|
||||
public class TipCalcSample : ISample
|
||||
{
|
||||
public string Title => "Xamarin.Forms TipCalc";
|
||||
|
||||
public Ooui.Element CreateElement()
|
||||
{
|
||||
var page = new TipCalc.TipCalcPage();
|
||||
return page.GetOouiElement();
|
||||
}
|
||||
|
||||
public void Publish()
|
||||
{
|
||||
UI.Publish("/tipcalc", CreateElement);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue