XAML System.Reflection.TargetInvocationException -调用的目标引发了异常

umuewwlo  于 2023-02-27  发布在  其他
关注(0)|答案(1)|浏览(135)

我正在使用Xamarin为学校做一个项目。我对Xamarin不是很熟悉,所以我会尽力解释我的问题。我的应用程序有一个课程页面和一个添加新课程页面。当点击Add New Course时,我得到错误:System.Reflection.TargetInvocationException -调用的目标抛出了异常.我不知道我做错了什么,我花了10多个小时试图找出这个问题。有没有更好的方法来导航到所需的页面?
由于项目的要求,我只能使用Pie 9.0 - API 28在Android上运行
CoursesPage.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>

AddNewCoursePage.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>

CoursesModel.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();
    }
}

添加新课程模型
公共课新增课程模型: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);
    }
}

AddNewCoursePage.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));
    }
}
zbdgwd5y

zbdgwd5y1#

我想是xaml出错了。
CoursesPage.xaml

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

AddNewCoursePage.xaml
每次“模式=双向”删除。日期选择器除外。

相关问题