我正在编写简单的WPF计算器应用程序,现在我已经实现了一些基本功能,如加法,减法等。我已经意识到我的代码看起来很糟糕,一个类对所有东西都是可用的。我正在努力将一些代码(在每个按钮单击事件方法中)提取到其他类和方法,但没有任何结果。我知道SOLID原则,但作为初学者,我在执行它们时遇到一些困难,尤其是单一责任原则。 CalculationWindow是TextBox名称。如果你能告诉我如何将CalculationWindow.Text = CalculationWindow.Text + "8"
提取到另一个班级,我会很感激。
其中一些方法
namespace CalculatorApplication
{
public partial class MainWindow : Window
{
private double inputFirstDigit;
public string actionOperator;`
public MainWindow()
{
InitializeComponent();
}
private void NumberOneButton_Click(object sender, RoutedEventArgs e)
{
CalculationWindow.Text = CalculationWindow.Text + "1";
}
private void NumberTwoButton_Click(object sender, RoutedEventArgs e)
{
CalculationWindow.Text = CalculationWindow.Text + "2";
}
private void NumberThreeButton_Click(object sender, RoutedEventArgs e)
{
CalculationWindow.Text = CalculationWindow.Text + "3";
}
private void NumberFourButton_Click(object sender, RoutedEventArgs e)
{
CalculationWindow.Text = CalculationWindow.Text + "4";
}
private void NumberFiveButton_Click(object sender, RoutedEventArgs e)
{
CalculationWindow.Text = CalculationWindow.Text + "5";
}
private void NumberSixButton_Click(object sender, RoutedEventArgs e)
{
CalculationWindow.Text = CalculationWindow.Text + "6";
}
private void NumberSevenButton_Click(object sender, RoutedEventArgs e)
{
CalculationWindow.Text = CalculationWindow.Text + "7";
}
XML
<Window x:Class="CalculatorApplication.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
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"
xmlns:local="clr-namespace:CalculatorApplication"
mc:Ignorable="d"
Title="Calculator" Height="480" Width="350" ResizeMode="NoResize">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="25"/>
<RowDefinition Height="85"/>
<RowDefinition Height="65"/>
<RowDefinition Height="65"/>
<RowDefinition Height="65"/>
<RowDefinition Height="65"/>
<RowDefinition Height="80"/>
</Grid.RowDefinitions>
<Menu Grid.Row="0">
<MenuItem Header="File">
<MenuItem Header="Save to file"/>
</MenuItem>
<MenuItem Header="Median"/>
<MenuItem Header="Fibonacci Sequence"/>
</Menu>
<TextBox x:Name="CalculationWindow" TextAlignment="Right" FontSize="29.333" Grid.ColumnSpan="2" HorizontalAlignment="Right" Height="70" Margin="15,15,15,0" Grid.Row="1" VerticalAlignment="Center" Width="310" FontWeight="Bold"/>
<Button x:Name="BackspaceButton" Content="⌫" FontSize="20" HorizontalAlignment="Left" Margin="15,15,0,0" Grid.Row="2" VerticalAlignment="Top" Width="50" Height="50" Click="BackspaceButton_Click"/>
<Button x:Name="NumberSevenButton" Content="7" HorizontalAlignment="Left" Margin="15,15,0,0" Grid.Row="3" VerticalAlignment="Top" Width="50" Height="50" Click="NumberSevenButton_Click"/>
<Button x:Name="NumberFourButton" Content="4" HorizontalAlignment="Left" Margin="15,15,0,0" Grid.Row="4" VerticalAlignment="Top" Width="50" Height="50" Click="NumberFourButton_Click"/>
<Button x:Name="NumberOneButton" Content="1" HorizontalAlignment="Left" Margin="15,15,0,0" Grid.Row="5" VerticalAlignment="Top" Width="50" Height="50" Click="NumberOneButton_Click"/>
<Button x:Name="NumberZeroButton" Content="0" HorizontalAlignment="Left" Margin="15,15,0,0" Grid.Row="6" VerticalAlignment="Top" Width="115" Height="50" Click="NumberZeroButton_Click"/>
<Button x:Name="CButton" Content="C" HorizontalAlignment="Left" Margin="80,15,0,0" Grid.Row="2" VerticalAlignment="Top" Width="115" Height="50" Click="CButton_Click"/>
<Button x:Name="NumberEightButton" Content="8" HorizontalAlignment="Left" Margin="80,15,0,0" Grid.Row="3" VerticalAlignment="Top" Width="50" Height="50" Click="NumberEightButton_Click"/>
<Button x:Name="NumberFiveButton" Content="5" HorizontalAlignment="Left" Margin="80,15,0,0" Grid.Row="4" VerticalAlignment="Top" Width="50" Height="50" Click="NumberFiveButton_Click"/>
<Button x:Name="NumberTwoButton" Content="2" HorizontalAlignment="Left" Margin="80,15,0,0" Grid.Row="5" VerticalAlignment="Top" Width="50" Height="50" Click="NumberTwoButton_Click"/>
<Button x:Name="NumberNineButton" Content="9" HorizontalAlignment="Left" Margin="145,15,0,0" Grid.Row="3" VerticalAlignment="Top" Width="50" Height="50" Click="NumberNineButton_Click"/>
<Button x:Name="NumberSixButton" Content="6" HorizontalAlignment="Left" Margin="145,15,0,0" Grid.Row="4" VerticalAlignment="Top" Width="50" Height="50" Click="NumberSixButton_Click"/>
<Button x:Name="NumberThreeButton" Content="3" HorizontalAlignment="Left" Margin="145,15,0,0" Grid.Row="5" VerticalAlignment="Top" Width="50" Height="50" Click="NumberThreeButton_Click"/>
<Button x:Name="CommaButton" Content="," HorizontalAlignment="Left" Margin="145,15,0,0" Grid.Row="6" VerticalAlignment="Top" Width="50" Height="50" Click="CommaButton_Click"/>
<Button x:Name="PlusMinusButton" Content="±" HorizontalAlignment="Left" Margin="210,15,0,0" Grid.Row="2" VerticalAlignment="Top" Width="50" Height="50" Click="PlusMinusButton_Click"/>
<Button x:Name="DivisionButton" Content="/" HorizontalAlignment="Left" Margin="210,15,0,0" Grid.Row="3" VerticalAlignment="Top" Width="50" Height="50" Click="DivisionButton_Click"/>
<Button x:Name="MultiplicationButton" Content="*" HorizontalAlignment="Left" Margin="210,15,0,0" Grid.Row="4" VerticalAlignment="Top" Width="50" Height="50" Click="MultiplicationButton_Click"/>
<Button x:Name="SubstractionButton" Content="-" HorizontalAlignment="Left" Margin="210,15,0,0" Grid.Row="5" VerticalAlignment="Top" Width="50" Height="50" Click="SubstractionButton_Click"/>
<Button x:Name="AdditionButton" Content="+" HorizontalAlignment="Left" Margin="210,15,0,0" Grid.Row="6" VerticalAlignment="Top" Width="50" Height="50" Click="AdditionButton_Click"/>
<Button x:Name="SqrtButton" Content="√" HorizontalAlignment="Left" Margin="275,15,0,0" Grid.Row="2" VerticalAlignment="Top" Width="50" Height="50" Click="SqrtButton_Click"/>
<Button x:Name="PercetangeButton" Content="%" HorizontalAlignment="Left" Margin="275,15,0,0" Grid.Row="3" VerticalAlignment="Top" Width="50" Height="50" Click="PercetangeButton_Click"/>
<Button x:Name="ReciprocalButton" Content="1/x" HorizontalAlignment="Left" Margin="275,15,0,0" Grid.Row="4" VerticalAlignment="Top" Width="50" Height="50" Click="ReciprocalButton_Click"/>
<Button x:Name="ResultButton" Grid.RowSpan="2" Content="=" HorizontalAlignment="Left" Margin="275,15,0,0" Grid.Row="5" VerticalAlignment="Top" Width="50" Height="115" Click="ResultButton_Click"/>
</Grid>
答案 0 :(得分:0)
你可以在MainWindow()的构造函数中作为依赖注入另一个类,如:
private OtherClass otherClass;
public MainWindow(OtherClass otherClass)
{
InitializeComponent();
this.otherClass = otherClass;
}
然后您可以在MainWindow.xaml.cs
中随处引用它