Skip to content

Commit 0de31c7

Browse files
authored
Merge pull request #624 from morning4coffe-dev/dev/doti/tip-calc
Migrate `TipCalc` sample
2 parents 7fe8ed6 + 6c372c3 commit 0de31c7

File tree

80 files changed

+2580
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

80 files changed

+2580
-0
lines changed

10.0/Apps/TipCalc/README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
---
2+
name: .NET MAUI - TipCalc
3+
description: TipCalc is based on an existing iOS and Android sample but has been completely rewritten for .NET MAUI using XAML and data-binding.
4+
page_type: sample
5+
languages:
6+
- csharp
7+
- xaml
8+
products:
9+
- maui
10+
urlFragment: tipcalc
11+
---
12+
# TipCalc
13+
14+
TipCalc is based on an existing iOS and Android sample but has been completely rewritten for .NET MAUI
15+
using XAML and data-binding. This new version now builds for iOS, Android, Windows, and Mac Catalyst.
16+
17+
TipCalc lets you type in a food-and-drink subtotal
18+
and a post-tax total from your restaurant bill and then select a tip percentage. It applies the percentage
19+
to the food-and-drink subtotal and adds the result to the post-tax total, rounded to the nearest quarter.
20+
21+
The calculations are handled in a *TipCalcModel* class, and the entire user interface is realized in
22+
XAML in the TipCalcPage.xaml file. Two data-binding value converters help massage the data between the data model and the XAML file.
23+
24+
![TipCalc application screenshot](Screenshots/tip-calc.png "TipCalc application screenshot")
25+
280 KB
Loading

10.0/Apps/TipCalc/TipCalc.sln

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.14.36221.1 d17.14
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TipCalc", "TipCalc\TipCalc.csproj", "{0251031F-61EE-4CC8-997A-1448E7371A7A}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{0251031F-61EE-4CC8-997A-1448E7371A7A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{0251031F-61EE-4CC8-997A-1448E7371A7A}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{0251031F-61EE-4CC8-997A-1448E7371A7A}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{0251031F-61EE-4CC8-997A-1448E7371A7A}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {35478027-B00E-4FDC-B290-9BA0281F02F1}
24+
EndGlobalSection
25+
EndGlobal

10.0/Apps/TipCalc/TipCalc/App.xaml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version = "1.0" encoding = "UTF-8" ?>
2+
<Application xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
4+
xmlns:local="clr-namespace:TipCalc"
5+
x:Class="TipCalc.App">
6+
<Application.Resources>
7+
<ResourceDictionary>
8+
<ResourceDictionary.MergedDictionaries>
9+
<ResourceDictionary Source="Resources/Styles/Colors.xaml" />
10+
<ResourceDictionary Source="Resources/Styles/Styles.xaml" />
11+
</ResourceDictionary.MergedDictionaries>
12+
</ResourceDictionary>
13+
</Application.Resources>
14+
</Application>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
namespace TipCalc;
2+
3+
public partial class App : Application
4+
{
5+
public App()
6+
{
7+
InitializeComponent();
8+
}
9+
10+
protected override Window CreateWindow(IActivationState? activationState)
11+
{
12+
return new Window(new TipCalcPage());
13+
}
14+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using System.Globalization;
2+
3+
namespace TipCalc;
4+
5+
public class DoubleRoundingConverter : IValueConverter
6+
{
7+
public object? Convert(object? value, Type targetType,
8+
object? parameter, CultureInfo culture)
9+
{
10+
return Round(value is double d ? d : 0, parameter);
11+
}
12+
13+
public object? ConvertBack(object? value, Type targetType,
14+
object? parameter, CultureInfo culture)
15+
{
16+
return Round(value is double d ? d : 0, parameter);
17+
}
18+
19+
double Round(double number, object? parameter)
20+
{
21+
double precision = 1;
22+
23+
// Assume parameter is string encoding precision.
24+
if (parameter is string s && double.TryParse(s, out var parsed))
25+
{
26+
precision = parsed;
27+
}
28+
return precision * Math.Round(number / precision);
29+
}
30+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using System.Globalization;
2+
3+
namespace TipCalc;
4+
5+
public class DoubleToStringConverter : IValueConverter
6+
{
7+
public object Convert(object? value, Type targetType,
8+
object? parameter, CultureInfo culture)
9+
{
10+
// Assumes value is double.
11+
double number = value is double d ? d : 0;
12+
13+
// Return empty string for a zero (good for Entry views).
14+
if (number == 0)
15+
{
16+
return "";
17+
}
18+
19+
return number.ToString();
20+
}
21+
22+
public object? ConvertBack(object? value, Type targetType,
23+
object? parameter, CultureInfo culture)
24+
{
25+
double number = 0;
26+
Double.TryParse(value as string, out number);
27+
return number;
28+
}
29+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using Microsoft.Extensions.Logging;
2+
3+
namespace TipCalc;
4+
5+
public static class MauiProgram
6+
{
7+
public static MauiApp CreateMauiApp()
8+
{
9+
var builder = MauiApp.CreateBuilder();
10+
builder
11+
.UseMauiApp<App>()
12+
.ConfigureFonts(fonts =>
13+
{
14+
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
15+
fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
16+
});
17+
18+
#if DEBUG
19+
builder.Logging.AddDebug();
20+
#endif
21+
22+
return builder.Build();
23+
}
24+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
3+
<application android:allowBackup="true" android:icon="@mipmap/appicon" android:roundIcon="@mipmap/appicon_round" android:supportsRtl="true"></application>
4+
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
5+
<uses-permission android:name="android.permission.INTERNET" />
6+
</manifest>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using Android.App;
2+
using Android.Content.PM;
3+
using Android.OS;
4+
5+
namespace TipCalc
6+
{
7+
[Activity(Theme = "@style/Maui.SplashTheme", MainLauncher = true, LaunchMode = LaunchMode.SingleTop, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize | ConfigChanges.Density)]
8+
public class MainActivity : MauiAppCompatActivity
9+
{
10+
}
11+
}

0 commit comments

Comments
 (0)