指定的转换不是 FreshMvvm Xamarin 中的有效异常
Specified cast is not valid exception in FreshMvvm Xamarin
我正在使用 FreshMvvm,在应用程序启动时出现异常。
Unhandled Exception: System.InvalidCastException: Specified cast is
not valid. : at (wrapper dynamic-method)
System.Object.7(intptr,intptr,intptr) : [ERROR] FATAL UNHANDLED
EXCEPTION: System.InvalidCastException: Specified cast is not valid.
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");
}
}
}
Student.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 ();
}
}
对应FreshBasePageModel
的每个页面都应该是Xamarin.Forms.Page
的子页面,例如Xamarin.Forms.ContentPage
。您可以使用 Visual Studio 中的 "Forms ContentPage" 模板创建它:
我正在使用 FreshMvvm,在应用程序启动时出现异常。
Unhandled Exception: System.InvalidCastException: Specified cast is not valid. : at (wrapper dynamic-method) System.Object.7(intptr,intptr,intptr) : [ERROR] FATAL UNHANDLED EXCEPTION: System.InvalidCastException: Specified cast is not valid.
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");
}
}
}
Student.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 ();
}
}
对应FreshBasePageModel
的每个页面都应该是Xamarin.Forms.Page
的子页面,例如Xamarin.Forms.ContentPage
。您可以使用 Visual Studio 中的 "Forms ContentPage" 模板创建它: