ex-ex-01-math-interpreter/MathInterpreter/MainWindow.axaml.cs
github-classroom[bot] 4b1294b32d
Initial commit
2025-06-03 15:18:01 +00:00

54 lines
1.4 KiB
C#

using System;
using System.Globalization;
using System.Threading.Tasks;
using Avalonia.Controls;
using Avalonia.Interactivity;
using MathInterpreter.Core;
using MsBox.Avalonia;
using MsBox.Avalonia.Base;
using MsBox.Avalonia.Enums;
namespace MathInterpreter;
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = this;
}
private async void Calculate(object sender, RoutedEventArgs e)
{
try
{
string input = ExpressionInput.Text ?? throw new InvalidOperationException("Input must not be empty");
var expression = new MathExpression(input);
Result.Text = expression.Result.ToString(CultureInfo.InvariantCulture);
}
catch (Exception ex)
{
string exStr = UnwrapException(ex);
await ShowError(exStr);
}
}
private static string UnwrapException(Exception exception)
{
string message = exception.Message;
if (exception.InnerException != null)
{
message += $"{Environment.NewLine}Inner exception: {UnwrapException(exception.InnerException)}";
}
return message;
}
private static async ValueTask ShowError(string message)
{
IMsBox<ButtonResult> box = MessageBoxManager.GetMessageBoxStandard("Error", message);
await box.ShowAsync();
}
}