Se podría forzar la utilización del IFormatProvider desde el constructor MainWindow() y se eliminaría la mayor parte de conversiones.
public partial class MainWindow : Window
{
SelectedOperator selectedOperator;
Double primerNumero;
Double segundoNumero;
public MainWindow()
{
InitializeComponent();
acButton.Click += AcButton_Click;
negativeButton.Click += NegativeButton_Click;
percentegeButton.Click += PercentegeButton_Click;
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
}
private void PercentegeButton_Click(object sender, RoutedEventArgs e)
{
if (double.TryParse(resultLabel.Content.ToString(), out double auxiliar))
{
resultLabel.Content = auxiliar / 100;
}
}
private void NegativeButton_Click(object sender, RoutedEventArgs e)
{
if (double.TryParse(resultLabel.Content.ToString(), out double auxiliar))
{
resultLabel.Content = auxiliar * -1;
}
}
private void AcButton_Click(object sender, RoutedEventArgs e)
{
resultLabel.Content = "0";
}
private void OperationButton_Click(object sender, RoutedEventArgs e)
{
primerNumero = Convert.ToDouble(resultLabel.Content);
resultLabel.Content = "0";
if (sender == asteriskButton)
selectedOperator = SelectedOperator.Multiplication;
if (sender == divisionButton)
selectedOperator = SelectedOperator.Division;
if (sender == plusButton)
selectedOperator = SelectedOperator.Addition;
if (sender == minusButton)
selectedOperator = SelectedOperator.Sustraction;
}
private void NumberButton_Click(object sender, RoutedEventArgs e)
{
if (resultLabel.Content.ToString().Equals("0"))
{
resultLabel.Content = (sender as Button).Content.ToString();
}
else
{
resultLabel.Content = $"{resultLabel.Content}{(sender as Button).Content.ToString()}";
}
}
public enum SelectedOperator
{
Addition,
Sustraction,
Multiplication,
Division
}
private void equalButton_Click(object sender, RoutedEventArgs e)
{
segundoNumero = Convert.ToDouble(resultLabel.Content);
switch (selectedOperator)
{
case SelectedOperator.Addition:
resultLabel.Content = (primerNumero + segundoNumero).ToString();
break;
case SelectedOperator.Division:
resultLabel.Content = (primerNumero / segundoNumero).ToString();
break;
case SelectedOperator.Multiplication:
resultLabel.Content = (primerNumero * segundoNumero).ToString();
break;
case SelectedOperator.Sustraction:
resultLabel.Content = (primerNumero - segundoNumero).ToString();
break;
}
}
private void dotButton_Click(object sender, RoutedEventArgs e)
{
if (!resultLabel.Content.ToString().Contains("."))
{
resultLabel.Content = $"{resultLabel.Content}.";
}
}
}