Arvind Chourasiya
Arvind Chourasiya

Reputation: 17422

Specified cast is not valid exception in FreshMvvm Xamarin

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

Upvotes: 1

Views: 1226

Answers (1)

foxanna
foxanna

Reputation: 1570

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

Upvotes: 2

Related Questions