【发布时间】:2021-05-06 22:48:02
【问题描述】:
我正在使用 Xamarin.Forms MVVM 和 sqlLite-net 来制作共享移动应用程序。
我收到一个错误,因为 ObservableCollection 中不存在“AddRange”,或者我使用该函数有误。
我要做的就是填写我的 ObservableCollection 列表。我必须手动添加使用循环吗?它奇怪的 ObservableCollection 有一个“添加”方法,但无法填充
错误:“ObservableCollection”不包含“AddRange”的定义,并且找不到接受“ObservableCollection”类型的第一个参数的可访问扩展方法“AddRange”(您是否缺少使用指令还是程序集引用?)
使用以下参考:
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using TestApp01_MVVM_Basic.Models;
using Xamarin.CommunityToolkit.ObjectModel;
using Xamarin.Forms;
using System.Collections.Specialized;
using TestApp01_MVVM_Basic.Services;
using System.Collections.Generic;
using System.Linq;
下面我正在创建我的 ObservableCollection。我在线收到错误:AddRange()
public ObservableCollection<ProductModel> ProductsList { get; set; }
public ProductViewModel()
{
ProductsList = new ObservableCollection<ProductModel>();
}
public async Task MyDisplayList()
{
var getData = ProductService.DisplayProduct();
ProductsList.AddRange(getData);
}
在服务类中,我有以下 DisplayProduct() 方法。从数据库返回一个列表
public static async Task<IEnumerable<ProductModel>> DisplayProduct()
{
await Init();
var GetProduct = await db.Table<ProductModel>().ToListAsync();
return GetProduct;
}
【问题讨论】:
-
System.Collections.ObjectModel.ObservableCollection<T>没有AddRange方法。您可以改用InsertItem。这里是docs
标签: c# xamarin xamarin.forms