重构多个事件处理程序以使代码可维护
本文关键字:代码 维护 程序 事件处理 重构 | 更新日期: 2023-09-27 18:23:35
重构下面各个按钮单击事件的最佳方式是什么?有可能吗。他们都在做同样的事情,而单独改变他们每个人都是一件痛苦的事。
主窗口.xaml.cs
private void Button1_Click(object sender, RoutedEventArgs e)
{
Button1_Add();
}
private void Button1_Add()
{
var tb = Keyboard.FocusedElement as TextBox;
try
{
var keypadObject = new Keypad();
keypadObject.AppendValue(tb, Button1.Content.ToString());
}
catch (Exception)
{
TotalTextBox.Focus();
}
}
private void Button2_Click(object sender, RoutedEventArgs e)
{
Button2_Add();
}
private void Button2_Add()
{
var tb = Keyboard.FocusedElement as TextBox;
try
{
var keypadObject = new Keypad();
keypadObject.AppendValue(tb, Button2.Content.ToString());
}
catch (Exception)
{
TotalTextBox.Focus();
}
}
private void Button3_Click(object sender, RoutedEventArgs e)
{
Button3_Add();
}
private void Button3_Add()
{
var tb = Keyboard.FocusedElement as TextBox;
try
{
var keypadObject = new Keypad();
keypadObject.AppendValue(tb, Button3.Content.ToString());
}
catch (Exception)
{
TotalTextBox.Focus();
}
}
private void Button4_Click(object sender, RoutedEventArgs e)
{
Button4_Add();
}
private void Button4_Add()
{
var tb = Keyboard.FocusedElement as TextBox;
try
{
var keypadObject = new Keypad();
keypadObject.AppendValue(tb, Button4.Content.ToString());
}
catch (Exception)
{
TotalTextBox.Focus();
}
}
private void Button5_Click(object sender, RoutedEventArgs e)
{
Button5_Add();
}
private void Button5_Add()
{
var tb = Keyboard.FocusedElement as TextBox;
try
{
var keypadObject = new Keypad();
keypadObject.AppendValue(tb, Button5.Content.ToString());
}
catch (Exception)
{
TotalTextBox.Focus();
}
}
private void Button6_Click(object sender, RoutedEventArgs e)
{
Button6_Add();
}
private void Button6_Add()
{
var tb = Keyboard.FocusedElement as TextBox;
try
{
var keypadObject = new Keypad();
keypadObject.AppendValue(tb, Button6.Content.ToString());
}
catch (Exception)
{
TotalTextBox.Focus();
}
}
private void Button7_Click(object sender, RoutedEventArgs e)
{
Button7_Add();
}
private void Button7_Add()
{
var tb = Keyboard.FocusedElement as TextBox;
try
{
var keypadObject = new Keypad();
keypadObject.AppendValue(tb, Button7.Content.ToString());
}
catch (Exception)
{
TotalTextBox.Focus();
}
}
private void Button8_Click(object sender, RoutedEventArgs e)
{
Button8_Add();
}
private void Button8_Add()
{
var tb = Keyboard.FocusedElement as TextBox;
try
{
var keypadObject = new Keypad();
keypadObject.AppendValue(tb, Button8.Content.ToString());
}
catch (Exception)
{
TotalTextBox.Focus();
}
}
private void Button9_Click(object sender, RoutedEventArgs e)
{
Button9_Add();
}
private void Button9_Add()
{
var tb = Keyboard.FocusedElement as TextBox;
try
{
var keypadObject = new Keypad();
keypadObject.AppendValue(tb, Button9.Content.ToString());
}
catch (Exception)
{
TotalTextBox.Focus();
}
}
private void Button0_Click(object sender, RoutedEventArgs e)
{
Button0_Add();
}
private void Button0_Add()
{
var tb = Keyboard.FocusedElement as TextBox;
try
{
var keypadObject = new Keypad();
keypadObject.AppendValue(tb, Button0.Content.ToString());
}
catch (Exception)
{
TotalTextBox.Focus();
}
}
Keypad.cs
public void AppendValue(TextBox tb, string valueToAppend)
{
if (tb.Text.Length < 6 && tb.Name != "RemainingTextBox")
{
if (tb.Text.IndexOf(".") == -1)
{
int beforeDotLength = tb.Text.Length;
if (beforeDotLength < 3)
{
tb.AppendText(valueToAppend);
}
}
else if (tb.Text.IndexOf(".") > -1)
{
int dotPosition = tb.Text.IndexOf(".");
int afterDotLength = tb.Text.Substring(dotPosition).Length;
if (afterDotLength < 3)
{
tb.AppendText(valueToAppend);
}
}
}
}
您可以从sender
参数中获得当前点击的按钮,因为它是当前点击的按键,只需将其转换为(Button)
:
private void Button_Add_Click(object sender, RoutedEventArgs e)
{
Button_Add((Button)sender);
}
private void Button_Add(Button button)
{
var tb = Keyboard.FocusedElement as TextBox;
try
{
var keypadObject = new Keypad();
keypadObject.AppendValue(tb, button.Content.ToString());
}
catch (Exception)
{
TotalTextBox.Focus();
}
}
然后使用Button_Add_Click
作为所有按钮的事件处理程序。
p.S.:(Button)sender
强制转换如果在错误的(而不是Button)处理程序中使用,可能会失败,因此您应该为这种情况做好准备(捕获异常-通知用户或至少日志)
P.P.S:考虑到这是WPF应用程序,您可能需要考虑使用MVVM设计模式。它有陡峭的学习曲线,一开始可能看起来过于复杂,没有任何优势,但它将允许您创建更健壮、耦合更少的应用程序。