我正在使用 Xamarin 开发一个学校项目。我对 Xamarin 不太熟悉,所以我会尽力解释我的问题。我的应用程序有一个课程页面和一个添加新课程页面。单击
Add New Course
时,出现错误: System.Reflection.TargetInitationException - 调用目标已引发异常。我不知道我做错了什么,并且花了 10 多个小时试图解决这个问题。有没有更好的方法导航到所需页面?
出于项目的要求,我仅限于使用 Pie 9.0 - API 28 在 Android 上运行
课程页面.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>
课程模型.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 { 私有字符串课程名称;
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.xaml.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
每个“Mode=TwoWay”删除。除了日期选择器 .