+-

我收到此未处理的异常错误:
An unhandled exception of type ‘System.Reflection.TargetInvocationException’ occurred in PresentationFramework.dll.
Additional information: Exception has been thrown by the target of an invocation.
简而言之,这是我的代码.它是两个文本框的计算器,当用户按下WPF中的-,/,x按钮时,它们应该一起成为一个新的答案.
public partial class MainWindow : Window
{
public string numberInString
{
get { return TextDoos.Text; }
set { TextDoos.Text = value; }
}
public MainWindow()
{
if(TextDoos.Text == "")
{
if(TextDoos2.Text == "")
{
RekenFunctie(TextDoos.Text, TextDoos2.Text);
}
}
}
}
public int RekenFunctie(string numberInString, string numberInString)
{
int antwoord;
int getal = Convert.ToInt32(numberInString);
int getal2 = Convert.ToInt32(numberInString2);
if (Buttons.IsPressed) // This is the + button, there are also -,x,/ buttons.
{
antwoord = getal + getal2;
return antwoord;
}
}
我不明白为什么它不起作用…
最佳答案
您错过了MainWindow构造函数中的 InitializeComponent()调用;
public MainWindow()
{
InitializeComponent();
button1.Click += button1_click; //'+' button
}
private void button1_click(object sender, RoutedEventArgs e)
{
int antwoord;
int getal = Convert.ToInt32(TextDoos.Text);
int getal2 = Convert.ToInt32(TextDoos2.Text);
antwoord = getal + getal2;
resultTextBox.Text = antwoord ;
}
无论如何,您的代码很奇怪. RekenFunctie进行了一些计算,但是您可以从构造函数中调用它.因此,您只运行一次此代码,但是我认为您的用户希望与您的计算器进行交互.
我认为您应该阅读有关Button.Click事件的信息.
点击查看更多相关文章
转载注明原文:C#-TargetInvocationException发生,但我绝对不知道为什么 - 乐贴网