【问题标题】:Specified cast is not valid exception in FreshMvvm Xamarin指定的强制转换在 FreshMvvm Xamarin 中是无效的异常
【发布时间】: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


【解决方案1】:

FreshBasePageModel 对应的每个页面都应该是Xamarin.Forms.Page 的子页面,例如Xamarin.Forms.ContentPage。您可以在 Visual Studio 中使用“Forms ContentPage”模板创建它:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-13
    • 1970-01-01
    • 2020-07-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-20
    相关资源
    最近更新 更多