指定的强制转换不是FreshMvvm Xamarin中的无效异常

时间:2018-10-23 09:30:47

标签: c# xamarin mvvm xamarin.forms freshmvvm

我正在使用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");
            }
        }
  }

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

1 个答案:

答案 0 :(得分:2)

const routes = () => ( <Switch> <Route path="/" exact component={Home}></Route> <Route path="/about" component={About}></Route> <Route path="/post" component={Posts}></Route> <Route component={PageNotFound}> </Route>*/} </Switch> ) export default routes; 对应的每个页面都应是FreshBasePageModel的子页面,例如Xamarin.Forms.Page。您可以在Visual Studio中使用“表单ContentPage”模板创建它:enter image description here