问题描述
- 编译下面这段代码的时候,提示上下问不存在p1,为什么呢,要怎样修改才能通过呢?
-
private void Window_Loaded_1(object sender, RoutedEventArgs e)
{
person p1 = new person();
p1.Age = 18;
p1.Name = "Hello";
txtbName.DataContext = p1;
txtbAge.DataContext = p1;
}private void Age_Click(object sender, RoutedEventArgs e) { p1.Age++; } private void ShowAge_Click(object sender, RoutedEventArgs e) { MessageBox.Show(p1.Age.tostring()); } }
解决方案
p1的作用域在哪呢?仅限于Window_Loaded_1方法内部,出了这个方法p1就不存在了。下面当然提示不存在p1,
时间: 2024-10-30 08:31:10