Wpf应用程序计算2000年我的年龄时出错



我正在为家庭作业开发一个WPF应用程序,它计算我在2000年的年龄。

由于某种原因,它没有识别clickButton中的值。这就是我尝试过的:

CS-

namespace BirthdayApp
{
public partial class MainWindow: Window
{
public MainWindow()
{
int curDate = 2020;
InitializeComponent();
message1.Text = "The year is " + curDate;
}
private void clickButton(object sender, RoutedEventArgs e)
{
int curDate = 2020;
message2.Text = ("You were ") + (value - curDate) + " in the year 2000";
}
}
}

XAML

<Grid>
<TextBox x:Name="value"/>
<Label x:Name="label"/>
<Button x:Name="button"/>
<TextBlock x:Name="message2"/>
<TextBlock x:Name="message1"/>
</Grid>

您的问题是在clickButton中通过指定值引用TextBox的名称。您想从TextBox访问text。您可以通过以下方式完成:

message2.Text = ("You were ") + (value.Text - curDate) + " in the year 2000";

相关内容

最新更新