系统.反射TargetInvocationException-调用的目标引发了异常



我正在为学校做一个使用Xamarin的项目。我对Xamarin不是很熟悉,所以我会尽力解释我的问题。我的应用程序有一个课程页面和一个添加新课程页面。当单击Add New Course时,我得到错误:系统。反射TargetInvocationException-调用的目标引发了异常。我不知道自己做错了什么,花了10多个小时试图弄清楚这一点。有没有更好的方法来导航到所需的页面?

对于项目的要求,我仅限于在Android上运行Pie 9.0-API 28

课程页码.xaml

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
xmlns:viewmodels="clr-namespace:FinalTestProject.ViewModels" 
xmlns:model="clr-namespace:FinalTestProject.Models"
x:Class="FinalTestProject.Views.CoursesPage"
Title="{Binding Title}">
<ContentPage.BindingContext>
<viewmodels:CoursesModel/>
</ContentPage.BindingContext>
<ContentPage.ToolbarItems>
<ToolbarItem Text="Add New Course" Command="{Binding NavigateToAddNewCoursePageCommand}"/>
</ContentPage.ToolbarItems>
<ListView
BackgroundColor="Transparent"
CachingStrategy="RecycleElement"
HasUnevenRows="True"
IsPullToRefreshEnabled="True"
ItemsSource="{Binding Course}"
IsRefreshing="{Binding IsBusy, Mode=OneWay}"
RefreshCommand="{Binding RefreshCommand}"
RefreshControlColor="Red"
SelectionMode="None"
SeparatorVisibility="None">
<ListView.ItemTemplate>
<DataTemplate x:DataType="model:Course">
<ViewCell>
<ViewCell.ContextActions>
<MenuItem/>
</ViewCell.ContextActions>
<Grid Padding="10">
<Frame CornerRadius="20" HasShadow="True">
<StackLayout>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
</Grid>
<Button Text="Delete" WidthRequest="100" Grid.Column="1"></Button>
<Label FontSize="Medium"
Grid.Column="0"
Text="{Binding CourseId}"/>
<Label FontSize ="Small"
Text="{Binding CourseName}"/>
<Label FontSize="Small"
Text="{Binding InstructorFirstName}"/>
<Label FontSize="Small"
Text="{Binding InstructorLastName}"/>
<Label FontSize="Small"
Text="{Binding AssessmentType}"/>
</StackLayout>
</Frame>
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>

添加新课程页面.xaml

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
xmlns:viewmodels="clr-namespace:FinalTestProject.ViewModels"
x:DataType="viewmodels:AddNewCourseModel"
x:Class="FinalTestProject.Views.AddNewCoursePage"
Title="{Binding Title}">
<ContentPage.BindingContext>
<viewmodels:AddNewCourseModel/>
</ContentPage.BindingContext>
<ContentPage.Content>
<ScrollView>
<StackLayout Spacing="3" Padding="15">
<Label Text="Course Name" FontSize="Small"/>
<Entry Text="{Binding CourseName, Mode=TwoWay}" FontSize="Small"/>
<Label Text="Instructor's First Name" FontSize="Small"/>
<Entry Text="{Binding InstructorFirstName, Mode=TwoWay}" FontSize="Small"/>
<Label Text="Instructor's Last Name" FontSize="Small"/>
<Entry Text="{Binding InstructorLastName, Mode=TwoWay}" FontSize="Small"/>
<Label Text="Notes" FontSize="Small"/>
<Editor Text="{Binding Notes, Mode=TwoWay}" FontSize="Small" AutoSize="TextChanges"/>
<Label Text="Start Date" FontSize="Small"/>
<DatePicker Date="{Binding CourseStartDate}"/>
<Label Text="End Date" FontSize="small"/>
<DatePicker Date="{Binding CourseEndDate}"/>
<Label Text="Assessment Type" FontSize="Small"/>
<Picker BindingContext="{Binding }">
<Picker.Items>
<x:String>Objective</x:String>
<x:String>Performance</x:String>
</Picker.Items>
</Picker>
<StackLayout Orientation="Horizontal">
<Button Text="Save" Command="{Binding AddCourseCommand}"  HorizontalOptions="FillAndExpand"></Button>
<Button Text="Cancel" Command="{Binding CancelCommand}" HorizontalOptions="FillAndExpand"></Button>
</StackLayout>
</StackLayout>
</ScrollView>
</ContentPage.Content>

课程Model.cs

public class CoursesModel : BaseViewModel
{
public ObservableRangeCollection<Course> AllCourses { get; set; }
public AsyncCommand RefreshCommand { get; }
public AsyncCommand NavigateToAddNewCoursePageCommand { get; }
public CoursesModel()
{
Title = "Courses";
AllCourses = new ObservableRangeCollection<Course>();
RefreshCommand = new AsyncCommand(Refresh);
NavigateToAddNewCoursePageCommand = new AsyncCommand(NavigateToAddNewCoursePage);
}
async Task NavigateToAddNewCoursePage()
{
await Shell.Current.GoToAsync(nameof(AddNewCoursePage));
}
async Task Refresh()
{
Busy();
AllCourses.Clear();
var courses = await CourseService.GetAllCourses();
AllCourses.AddRange(courses);
NotBusy();
}
}

添加新课程模型

公共类AddNewCourseModel:BaseViewModel{私有字符串courseName;

public AsyncCommand AddCourseCommand { get; }
public AsyncCommand CancelCommand { get; }
public AddNewCourseModel()
{
Title = "Add New Course";
AddCourseCommand = new AsyncCommand(AddCourse);
CancelCommand = new AsyncCommand(Cancel);
}
async Task AddCourse()
{
var course = new Course()
{
CourseName = CourseName
};
await CourseService.AddCourse(course);
await Shell.Current.GoToAsync("CoursesPage");
}
async Task Cancel()
{
await Shell.Current.GoToAsync("CoursesPage");
}
public string CourseName
{
get => courseName;
set => SetProperty(ref courseName, value);
}
}

添加新课程页面.xaml.cs

[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class AddNewCoursePage : ContentPage
{
public AddNewCoursePage()
{
InitializeComponent();
}
}

AppShell.xam.cs

public partial class AppShell : Xamarin.Forms.Shell
{
public AppShell()
{
InitializeComponent();
Routing.RegisterRoute(nameof(CoursesPage), typeof(CoursesPage));
Routing.RegisterRoute(nameof(AddNewCoursePage), typeof(AddNewCoursePage));
Routing.RegisterRoute(nameof(TermsPage), typeof(TermsPage));
Routing.RegisterRoute(nameof(AddNewTermPage), typeof(AddNewTermPage));
}
}

我认为它在xaml上发生错误。

课程页码.xaml

ItemsSource="{Binding Course , Mode=OneWayToSource}"

添加新课程页面.xaml

每";模式=双向;删去除日期选择器.

相关内容

最新更新