有没有办法通过读取 csv 文件来填充 Xamarin.Forms 中的 ObservableCollection?

Is there a way to populate an ObservableCollection in Xamarin.Forms by reading a csv file?

我下面有一个学生 class,我在 ViewModel class 中使用 ObservableCollection 在 ListView 中显示学生。我可以从 csv/spreadsheet 中读取数据来填充 ObservableCollection 吗?

public class Student
{
    public string Firstname { get; set; }
    public string Surname { get; set; }
    public string Module { get; set; }

    public Project(string Firstname, string Surname, string Module)
    {
        this.Firstname = Firstname;
        this.Surname = Surname;
        this.Module = Module;
}

public class ProjectListViewModel
{
    public ObservableCollection<Student> Students { get; set; }
    public ProjectListViewModel()
    {
        Students = new ObservableCollection<Project>();
        Students.Add(new Student("Ben", "Ledley", "Maths"));
        Students.Add(new Student("John", "Alex", "English"));
        Students.Add(new Student("Sam", "Craig", "Biology"));
}
        
}

是的,您可以通过从 NuGet 包安装 CsvHelper 来使用 CsvHelper 读取 csv 数据。

您可以创建一个 .csv 文件并将其放入 Xamarin.Forms 项目中。 还有一个类似的例子,读一个.csv文件data.You可以参考下面的代码

例如test.csv的内容如下:(设置Build ActionEmbedded resource):

ID,Name,Age
1,A,11
2,B,12
3,C,13
4,D,14
5,E,15

物品型号为Item.cs

public  class Item
{
    public string ID { get; set; }

    public string Name { get; set; }

    public string Age { get; set; }
}

MainPage.xaml

<StackLayout>
    <ListView  x:Name="mListView" HasUnevenRows="True">
        <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
                <StackLayout Orientation="Horizontal" HorizontalOptions="Fill" BackgroundColor="Olive">
                    <StackLayout Orientation="Vertical">
                        <Label Text = "{Binding Name}" FontSize="24" AbsoluteLayout.LayoutBounds="0.25, 0.25, 400, 40"/>
                        <Label Text = "{Binding Age}" AbsoluteLayout.LayoutBounds="50, 35, 200, 25"/>
                    </StackLayout>
                </StackLayout>
            </ViewCell>
        </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
    <Button Clicked="Button_Clicked" Text="Read data from CSV" />
</StackLayout>

MainPage.xaml.cs

   public partial class MainPage : ContentPage
    {
        public ObservableCollection<Item> items { get; set; }
        public MainPage()
        {
            InitializeComponent();

            items = new ObservableCollection<Item>();

            mListView.ItemsSource = items;
        }

        private void Button_Clicked(object sender, EventArgs e)
        {

            var assembly = Assembly.GetExecutingAssembly();
            var resourceName = "ReadFromExcel.test.csv";

            using (Stream stream = assembly.GetManifestResourceStream(resourceName))
            using (StreamReader reader = new StreamReader(stream))
            {
                //string result = reader.ReadToEnd();
                if (reader != null)
                {
                    using (var csv = new CsvReader(reader, CultureInfo.CurrentCulture))
                    {
                        while (csv.Read())
                        {
                            items.Add(new Item
                            {
                                ID = csv.GetField<string>(0),
                                Name = csv.GetField<string>(1),
                                Age = csv.GetField<string>(2)
                            });
                        }
                    }
                }

            }

        }
    }

注意:点击按钮后,我们可以从.CSV文件中读取数据。