如何解决重新加载列表视图项时“指定的演员表无效” - Xamarin Forms
我正在尝试制作一个将主题保存到 sqlite 数据库中的应用程序,但我遇到了一个异常:“指定的强制转换无效”。当我重新加载列表视图项目时会发生这种情况。 (Subjectviewmodel.UpdateSubjects())
我通过异步连接从 SubjectServices 静态类接收数据。
我将 contentpage 绑定上下文设置为此视图模型,然后将列表视图项源的绑定设置为 Subjectviewmodel 的 SubjectList。
XAML 代码:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:resx="clr-namespace:MyApp.Resources"
x:Class="MyApp.View.ShellPages.SubjectsPage">
<ContentPage.toolbaritems>
<ToolbarItem Text="{x:Static resx:AppResources.NewSubject}"
Command="{Binding SubjectNewSCommand}"/>
</ContentPage.toolbaritems>
<ContentPage.Content>
<ListView x:Name="subjectListView" ItemsSource="{Binding SubjectList}"
IsRefreshing="True" RefreshCommand="{Binding SubjectRefreshCommand}">
<ListView.ItemTemplate>
<DataTemplate>
<TextCell Text="{Binding Name}" Detail="{Binding AddedTime,StringFormat='{0:d}'}"/>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</ContentPage.Content>
</ContentPage>
主题视图模型:
using MyApp.Model;
using MyApp.View.ShellPages;
using MyApp.viewmodel.Commands;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Text;
namespace MyApp.viewmodel
{
public class Subjectviewmodel
{
public ObservableCollection<Subject> SubjectList { get; set; }
public SubjectPageRefreshCommand SubjectRefreshCommand { get; set; }
public SubjectNewSubjectCommand SubjectNewSCommand { get; set; }
public Subjectviewmodel()
{
SubjectRefreshCommand = new SubjectPageRefreshCommand(this);
SubjectNewSCommand = new SubjectNewSubjectCommand(this);
SubjectList = new ObservableCollection<Subject>();
}
public async void UpdateSubjects()
{
var subjectList = await SubjectServices.GetSubjects();
SubjectList.Clear();
foreach (var subject in subjectList)
SubjectList.Add(subject);
}
public async void Navigate()
{
await App.Current.MainPage.Navigation.PushAsync(new NewSubjectPage());
}
}
}
学科类别:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Threading.Tasks;
using sqlite;
namespace MyApp.Model
{
public class Subject
{
[PrimaryKey,AutoIncrement]
public int ID { get; set; }
[MaxLength(50)]
public string Name { get; set; }
public DateTime AddedTime { get; set; }
}
}
可能是什么问题?我试图找到任何解决方案,但没有成功。 我感谢任何形式的帮助。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。