basic design
This commit is contained in:
commit
e0adb55d43
8
.gitignore
vendored
Normal file
8
.gitignore
vendored
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
bin/
|
||||||
|
obj/
|
||||||
|
.idea/
|
||||||
|
.vs/
|
||||||
|
tmp/
|
||||||
|
temp/
|
||||||
|
packages/
|
||||||
|
*.user
|
||||||
8
SharpCalculator.Avalonia/App.axaml
Normal file
8
SharpCalculator.Avalonia/App.axaml
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
<Application xmlns="https://github.com/avaloniaui"
|
||||||
|
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||||
|
x:Class="SharpCalculator.Avalonia.App"
|
||||||
|
RequestedThemeVariant="Dark">
|
||||||
|
<Application.Styles>
|
||||||
|
<SimpleTheme />
|
||||||
|
</Application.Styles>
|
||||||
|
</Application>
|
||||||
23
SharpCalculator.Avalonia/App.axaml.cs
Normal file
23
SharpCalculator.Avalonia/App.axaml.cs
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
using Avalonia;
|
||||||
|
using Avalonia.Controls.ApplicationLifetimes;
|
||||||
|
using Avalonia.Markup.Xaml;
|
||||||
|
|
||||||
|
namespace SharpCalculator.Avalonia;
|
||||||
|
|
||||||
|
public partial class App : Application
|
||||||
|
{
|
||||||
|
public override void Initialize()
|
||||||
|
{
|
||||||
|
AvaloniaXamlLoader.Load(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void OnFrameworkInitializationCompleted()
|
||||||
|
{
|
||||||
|
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
|
||||||
|
{
|
||||||
|
desktop.MainWindow = new MainWindow();
|
||||||
|
}
|
||||||
|
|
||||||
|
base.OnFrameworkInitializationCompleted();
|
||||||
|
}
|
||||||
|
}
|
||||||
42
SharpCalculator.Avalonia/MainWindow.axaml
Normal file
42
SharpCalculator.Avalonia/MainWindow.axaml
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
<Window xmlns="https://github.com/avaloniaui"
|
||||||
|
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||||
|
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||||
|
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||||
|
mc:Ignorable="d" Width="400" Height="500"
|
||||||
|
x:Class="SharpCalculator.Avalonia.MainWindow"
|
||||||
|
Title="SharpCalculator"
|
||||||
|
FontFamily="Consolas"
|
||||||
|
FontSize="24">
|
||||||
|
<Grid Margin="16">
|
||||||
|
<Grid.RowDefinitions>4* 1* 4*</Grid.RowDefinitions>
|
||||||
|
<TextBox Name="Input" IsReadOnly="True"></TextBox>
|
||||||
|
<TextBlock Name="Output" Grid.Row="1" VerticalAlignment="Center">= 0</TextBlock>
|
||||||
|
<Grid Grid.Row="2">
|
||||||
|
<Grid.ColumnDefinitions> * * * * </Grid.ColumnDefinitions>
|
||||||
|
<Grid.RowDefinitions> * * * * * </Grid.RowDefinitions>
|
||||||
|
<Button Grid.Row="1" Grid.Column="0" Name="SevenButton" Click="NumberButton_OnClick">7</Button>
|
||||||
|
<Button Grid.Row="1" Grid.Column="1" Name="EightButton" Click="NumberButton_OnClick">8</Button>
|
||||||
|
<Button Grid.Row="1" Grid.Column="2" Name="NineButton" Click="NumberButton_OnClick">9</Button>
|
||||||
|
|
||||||
|
<Button Grid.Row="2" Grid.Column="0" Name="FourButton" Click="NumberButton_OnClick">4</Button>
|
||||||
|
<Button Grid.Row="2" Grid.Column="1" Name="FiveButton" Click="NumberButton_OnClick">5</Button>
|
||||||
|
<Button Grid.Row="2" Grid.Column="2" Name="SixButton" Click="NumberButton_OnClick">6</Button>
|
||||||
|
|
||||||
|
<Button Grid.Row="3" Grid.Column="0" Name="OneButton" Click="NumberButton_OnClick">1</Button>
|
||||||
|
<Button Grid.Row="3" Grid.Column="1" Name="TwoButton" Click="NumberButton_OnClick">2</Button>
|
||||||
|
<Button Grid.Row="3" Grid.Column="2" Name="ThreeButton" Click="NumberButton_OnClick">3</Button>
|
||||||
|
|
||||||
|
<Button Grid.Row="4" Grid.Column="0" Name="ZeroButton" Click="NumberButton_OnClick">0</Button>
|
||||||
|
<Button Grid.Row="4" Grid.Column="1" Name="PointButton" Click="NumberButton_OnClick">.</Button>
|
||||||
|
<Button Grid.Row="4" Grid.Column="2" Name="PowerButton" Click="OperationButton_OnClick">^</Button>
|
||||||
|
|
||||||
|
<Button Grid.Row="1" Grid.Column="3" Name="DivideButton" Click="OperationButton_OnClick">/</Button>
|
||||||
|
<Button Grid.Row="2" Grid.Column="3" Name="MultiplyButton" Click="OperationButton_OnClick">*</Button>
|
||||||
|
<Button Grid.Row="3" Grid.Column="3" Name="SubstractButton" Click="OperationButton_OnClick">-</Button>
|
||||||
|
<Button Grid.Row="4" Grid.Column="3" Name="AddButton" Click="OperationButton_OnClick">+</Button>
|
||||||
|
|
||||||
|
<Button Grid.Row="0" Grid.Column="3" Name="EqualButton" Click="OperationButton_OnClick">=</Button>
|
||||||
|
<Button Grid.Row="0" Grid.Column="2" Name="ClearButton" Click="ClearButton_OnClick">CLR</Button>
|
||||||
|
</Grid>
|
||||||
|
</Grid>
|
||||||
|
</Window>
|
||||||
90
SharpCalculator.Avalonia/MainWindow.axaml.cs
Normal file
90
SharpCalculator.Avalonia/MainWindow.axaml.cs
Normal file
@ -0,0 +1,90 @@
|
|||||||
|
using System;
|
||||||
|
using System.Globalization;
|
||||||
|
using System.Text;
|
||||||
|
using Avalonia.Controls;
|
||||||
|
using Avalonia.Interactivity;
|
||||||
|
|
||||||
|
namespace SharpCalculator.Avalonia;
|
||||||
|
|
||||||
|
public partial class MainWindow : Window
|
||||||
|
{
|
||||||
|
public MainWindow()
|
||||||
|
{
|
||||||
|
InitializeComponent();
|
||||||
|
}
|
||||||
|
|
||||||
|
private double _rezult;
|
||||||
|
private StringBuilder currentNumberB = new();
|
||||||
|
|
||||||
|
private double CompleteCurrentNumber()
|
||||||
|
{
|
||||||
|
var n = double.Parse(currentNumberB.ToString(), CultureInfo.InvariantCulture);
|
||||||
|
currentNumberB.Clear();
|
||||||
|
return n;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void UpdateOutput()
|
||||||
|
{
|
||||||
|
Output.Text = "= " + _rezult;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Func<double, double, double> prevOperation = (_, firstN) => firstN;
|
||||||
|
|
||||||
|
private void TryUpdateRezult(Func<double, double, double> operation, string text)
|
||||||
|
{
|
||||||
|
if (currentNumberB.Length > 0)
|
||||||
|
{
|
||||||
|
_rezult = prevOperation(_rezult, CompleteCurrentNumber());
|
||||||
|
prevOperation = operation;
|
||||||
|
Input.Text += " " + text + " ";
|
||||||
|
}
|
||||||
|
|
||||||
|
UpdateOutput();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void NumberButton_OnClick(object? sender, RoutedEventArgs e)
|
||||||
|
{
|
||||||
|
if (sender is not Button button)
|
||||||
|
throw new Exception();
|
||||||
|
string text = button.Content?.ToString()!;
|
||||||
|
Input.Text += text;
|
||||||
|
currentNumberB.Append(text);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void OperationButton_OnClick(object? sender, RoutedEventArgs e)
|
||||||
|
{
|
||||||
|
if (sender is not Button button)
|
||||||
|
throw new Exception();
|
||||||
|
string text = button.Content?.ToString()!;
|
||||||
|
switch (text)
|
||||||
|
{
|
||||||
|
case "+":
|
||||||
|
TryUpdateRezult((a, b) => a + b, text);
|
||||||
|
break;
|
||||||
|
case "-":
|
||||||
|
TryUpdateRezult((a, b) => a - b, text);
|
||||||
|
break;
|
||||||
|
case "*":
|
||||||
|
TryUpdateRezult((a, b) => a * b, text);
|
||||||
|
break;
|
||||||
|
case "/":
|
||||||
|
TryUpdateRezult((a, b) => a / b, text);
|
||||||
|
break;
|
||||||
|
case "^":
|
||||||
|
TryUpdateRezult(Math.Pow, text);
|
||||||
|
break;
|
||||||
|
case "=":
|
||||||
|
TryUpdateRezult((a, _) => a, "");
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
throw new Exception("incorrect button text: " + text);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void ClearButton_OnClick(object? sender, RoutedEventArgs e)
|
||||||
|
{
|
||||||
|
_rezult = 0;
|
||||||
|
UpdateOutput();
|
||||||
|
Input.Text = "";
|
||||||
|
}
|
||||||
|
}
|
||||||
20
SharpCalculator.Avalonia/Program.cs
Normal file
20
SharpCalculator.Avalonia/Program.cs
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
using Avalonia;
|
||||||
|
using System;
|
||||||
|
|
||||||
|
namespace SharpCalculator.Avalonia;
|
||||||
|
|
||||||
|
class Program
|
||||||
|
{
|
||||||
|
// Initialization code. Don't use any Avalonia, third-party APIs or any
|
||||||
|
// SynchronizationContext-reliant code before AppMain is called: things aren't initialized
|
||||||
|
// yet and stuff might break.
|
||||||
|
[STAThread]
|
||||||
|
public static void Main(string[] args) => BuildAvaloniaApp()
|
||||||
|
.StartWithClassicDesktopLifetime(args);
|
||||||
|
|
||||||
|
// Avalonia configuration, don't remove; also used by visual designer.
|
||||||
|
private static AppBuilder BuildAvaloniaApp()
|
||||||
|
=> AppBuilder.Configure<App>()
|
||||||
|
.UsePlatformDetect()
|
||||||
|
.LogToTrace();
|
||||||
|
}
|
||||||
18
SharpCalculator.Avalonia/SharpCalculator.Avalonia.csproj
Normal file
18
SharpCalculator.Avalonia/SharpCalculator.Avalonia.csproj
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
<PropertyGroup>
|
||||||
|
<OutputType>WinExe</OutputType>
|
||||||
|
<TargetFramework>net8.0</TargetFramework>
|
||||||
|
<LangVersion>12</LangVersion>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
<ImplicitUsings>disable</ImplicitUsings>
|
||||||
|
<BuiltInComInteropSupport>true</BuiltInComInteropSupport>
|
||||||
|
<AvaloniaUseCompiledBindingsByDefault>true</AvaloniaUseCompiledBindingsByDefault>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Avalonia" Version="11.0.5"/>
|
||||||
|
<PackageReference Include="Avalonia.Desktop" Version="11.0.5"/>
|
||||||
|
<PackageReference Condition="'$(Configuration)' == 'Debug'" Include="Avalonia.Diagnostics" Version="11.0.5"/>
|
||||||
|
<PackageReference Include="Avalonia.Themes.Simple" Version="11.0.5" />
|
||||||
|
</ItemGroup>
|
||||||
|
</Project>
|
||||||
16
SharpCalculator.sln
Normal file
16
SharpCalculator.sln
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
|
||||||
|
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SharpCalculator.Avalonia", "SharpCalculator.Avalonia\SharpCalculator.Avalonia.csproj", "{93E7D002-22D7-4C4F-AE6B-F1E0D938BCBA}"
|
||||||
|
EndProject
|
||||||
|
Global
|
||||||
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
|
Debug|Any CPU = Debug|Any CPU
|
||||||
|
Release|Any CPU = Release|Any CPU
|
||||||
|
EndGlobalSection
|
||||||
|
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||||
|
{93E7D002-22D7-4C4F-AE6B-F1E0D938BCBA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{93E7D002-22D7-4C4F-AE6B-F1E0D938BCBA}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{93E7D002-22D7-4C4F-AE6B-F1E0D938BCBA}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{93E7D002-22D7-4C4F-AE6B-F1E0D938BCBA}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
EndGlobalSection
|
||||||
|
EndGlobal
|
||||||
6
nuget.config
Normal file
6
nuget.config
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<configuration>
|
||||||
|
<packageSources>
|
||||||
|
<add key="local" value="./packages/" />
|
||||||
|
</packageSources>
|
||||||
|
</configuration>
|
||||||
Loading…
Reference in New Issue
Block a user