【发布时间】:2019-03-27 11:49:36
【问题描述】:
我正在使用 FreshMvvm,在应用程序启动时出现异常。
未处理的异常:System.InvalidCastException:指定的转换是 无效。 : at (包装动态方法) System.Object.7(intptr,intptr,intptr):[错误] 致命未处理 异常:System.InvalidCastException:指定的强制转换无效。
public App()
{
InitializeComponent();
var mainPage = FreshPageModelResolver.ResolvePageModel<StudentListPageModel>(); //Here getting exception
MainPage = new FreshNavigationContainer(mainPage);
}
StudentListPage.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 ();
}
}
【问题讨论】:
-
StudentListPage.xaml.cs有什么特别之处吗?什么是基类? -
基类是
FreshBasePageModel来自FreshMvvm包。我在这里粘贴的总文件没什么特别的。 -
我指的是页面,而不是 ViewModel。另外,为了清楚起见,
Student里面是什么? -
StudentListPage应该是Page或其子类型之一,而不是ContentView -
感觉自己很蠢我怎么加了 ContentView,但是你是鹰眼谢谢@foxanna
标签: c# xamarin mvvm xamarin.forms freshmvvm