1
votes

I am using FreshMvvm, getting exception at the starting of application.

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 file is empty

public partial class StudentListPage : ContentView
{
    public StudentListPage ()
    {
        InitializeComponent ();
    }
}
1
Is there anything special in StudentListPage.xaml.cs? What is the base class?foxanna
Base class is FreshBasePageModel comes from FreshMvvm package. Total file I have pasted here nothing special.R15
I meant the page, not the ViewModel. Also, just to make it clear, what is inside Student?foxanna
StudentListPage should be of type Page or one of its child, not ContentViewfoxanna
Feeling stupid how come I added ContentView, But you are hawk-eyed thanks @foxannaR15

1 Answers

2
votes

Every page that is corresponding to FreshBasePageModel should be a child of Xamarin.Forms.Page, Xamarin.Forms.ContentPage as an example. You can create it with "Forms ContentPage" template in Visual Studio: enter image description here