1

我正在使用 FreshMvvm,在应用程序启动时出现异常。

未处理的异常:System.InvalidCastException:指定的转换无效。:在(包装器动态方法)System.Object.7(intptr,intptr,intptr):[错误]致命的未处理异常:System.InvalidCastException:指定的强制转换无效。

public App()
{
   InitializeComponent();
   var mainPage = FreshPageModelResolver.ResolvePageModel<StudentListPageModel>(); //Here getting exception
   MainPage = new FreshNavigationContainer(mainPage);
}

学生列表页面.xaml

<StackLayout>
    <Label Text="{Binding StudentName}"  Font="20"/>
    <Label Text="{Binding StudentClass}" Font="20"/>
    <Label Text="{Binding City}"  HorizontalOptions="FillAndExpand"/>
</StackLayout>

StudentListPageModel.cs

public class StudentListPageModel : FreshBasePageModel
  {
        private Student _student;
        public StudentListPageModel()
        {
            _student = new Student();
        }

        public string StudentName
        {
            get { return _student.StudentName; }
            set
            {
                _student.StudentName = value;
                RaisePropertyChanged("StudentName");
            }
        }

        public string StudentClass
        {
            get { return _student.StudentClass; }
            set
            {

                _student.StudentClass = value;
                RaisePropertyChanged("StudentClass");
            }
        }

        public string City
        {
            get { return _student.City; }
            set
            {
                _student.City = value;
                RaisePropertyChanged("City");
            }
        }
  }

学生.cs

public class Student
{
    public string StudentName { get; set; }
    public string StudentClass { get; set; }
    public string City { get; set; }
}

StudentListPage.xaml.cs 文件为空

public partial class StudentListPage : ContentView
{
    public StudentListPage ()
    {
        InitializeComponent ();
    }
}
4

1 回答 1

2

例如,对应的每个页面都FreshBasePageModel应该是 的子级。您可以在 Visual Studio 中使用“Forms ContentPage”模板创建它:Xamarin.Forms.PageXamarin.Forms.ContentPage在此处输入图像描述

于 2018-10-23T10:07:34.400 回答