XAML 中的命名项在当前上下文中不存在



我正在做一个学校项目,我需要在XAML和C#代码之间绑定项。我已经在XAML文件中定义了名称,但C#代码报告某些项"不存在于当前上下文中"。C#中出现此错误的变量为:

StartDatePicker
EndDatePicker
InstructorName
InstructorEmail
InstructorPhone

我已经通过复制和粘贴XAML文件中的x:Name字段替换了这些项,清理了所有文件并重新生成了解决方案,但仍然收到错误。

受影响的C#代码块:

public CourseInfoPage(Courses selectedCourse)
{
InitializeComponent();
thisCourse = selectedCourse;
courseId = thisCourse.Id;
int courseInstructorId = thisCourse.Instructor;
GetCourseInstructor(courseInstructorId);
StartDatePicker.Date = thisCourse.RealStartDate;
EndDatePicker.Date = thisCourse.RealEndDate;
}
private void GetCourseInstructor(int courseInstructorId)
{
using (SQLiteConnection conn = new SQLiteConnection(App.DBLocation))
{
var instructorName = conn.Query<Instructors>($"SELECT FirstName, LastName FROM Instructors WHERE Id = {courseInstructorId}");
var email = conn.Query<Instructors>($"SELECT Email FROM Instructors WHERE Id = {courseInstructorId}");
var phone = conn.Query<Instructors>($"SELECT Phone FROM Instructors WHERE Id = {courseInstructorId}");
InstructorName.Text = instructorName.ToString();
InstructorEmail.Text = email.ToString();
InstructorPhone.Text = phone.ToString();
}
}

XAML集合视图:

<CollectionView x:Name="CourseInfoView">
<CollectionView.ItemTemplate>
<DataTemplate>
<Grid Padding="10">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Label Grid.Column="0"
Grid.Row="0"
Text="{Binding CourseID}"
<Label Grid.Column="1"
Grid.Row="0"
Text="{Binding CourseName}"
<Label Grid.Column="0"
Grid.Row="1"
Text="Start Date:"
<DatePicker x:Name="StartDatePicker"
MinimumDate="01/01/2020"
MaximumDate="12/31/2050"
Date="{Binding RealStartDate}"
DateSelected="OnDateSelected"
Grid.Column="1"
Grid.Row="1"
<DatePicker.Format>MM/dd/yyyy</DatePicker.Format>
</DatePicker>
<Label Grid.Column="0"
Grid.Row="2"
Text="End Date:"
<DatePicker x:Name="EndDatePicker"
MinimumDate="02/01/2020"
MaximumDate="01/01/2051"
Date="{Binding RealEndDate}"
DateSelected="OnDateSelected"
Grid.Column="1"
Grid.Row="2"
<DatePicker.Format>MM/dd/yyyy</DatePicker.Format>
</DatePicker>
<Label Grid.Column="0"
Grid.Row="3"
Text="Select a status:"
<Picker x:Name="picker"
Grid.Column="1"
Grid.Row="3">
<Picker.ItemsSource>
<x:Array Type="{x:Type x:String}">
<x:String>Not Started</x:String>
<x:String>In Progress</x:String>
<x:String>Completed</x:String>
<x:String>Not Passed</x:String>
</x:Array>
</Picker.ItemsSource>
</Picker>
<Label Grid.Column="0"
Grid.Row="4"
Text="Course Instructor:"
<Label Grid.Column="1"
Grid.Row="4"
x:Name="InstructorName"
<Label Grid.Column="0"
Grid.Row="5"
Text="Instructor Email:"
<Label Grid.Column="1"
Grid.Row="5"
x:Name="InstructorEmail"
<Label Grid.Column="0"
Grid.Row="6"
Text="Instructor Phone:"
<Label Grid.Column="1"
Grid.Row="6"
x:Name="InstructorPhone"
<Label Grid.Column="0"
Grid.Row="7"
Text="Course Description:"
<Label Grid.ColumnSpan="2"
Grid.Row="8"
Text="{Binding CourseDescription}"
<Button x:Name="courseNotesButton"
Text="View Course Notes"
Clicked="courseNotesButton_Clicked"/>
</Grid>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>

我确信我错过了一些简单的东西,但我看不出它是什么。任何人能提供的帮助都是超级的;谢谢

您使用的是DataTemplate,这意味着这些名称在后面的代码中不可用。模板是在运行时需要时膨胀的东西。在DataTemplate中引用这样的控件是不可能的。该模板将对基础集合中的每个项目重复使用。

您将希望了解如何使用MVVM和数据绑定。在全球范围内,这意味着;将所有的Course对象放入一个集合中,最好是一个ObservableCollection。然后将CollectionView.ItemsSource设置为该集合。

您不需要x:Name属性,而是使用{Binding MemberName}来填充正确的值。我看你已经在用了,所以这太棒了。

最新更新