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

Posted

技术标签:

【中文标题】有没有办法通过读取 csv 文件来填充 Xamarin.Forms 中的 ObservableCollection?【英文标题】:Is there a way to populate an ObservableCollection in Xamarin.Forms by reading a csv file? 【发布时间】:2021-12-27 17:16:02 【问题描述】:

我在下面有一个 Student 类,我在 ViewModel 类中使用 ObservableCollection 在 ListView 中显示学生。我是否可以从 csv/电子表格中读取数据来填充 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"));

        

【问题讨论】:

joshclose.github.io/CsvHelper 【参考方案1】:

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

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

例如test.csv的内容如下:(将Build Action设置为Embedded 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文件中读取数据。

【讨论】:

非常感谢您的帮助

以上是关于有没有办法通过读取 csv 文件来填充 Xamarin.Forms 中的 ObservableCollection?的主要内容,如果未能解决你的问题,请参考以下文章

有没有办法使用给定目录中的 numpy.genfromtxt 从 csv 读取数据?

通过 Python 向 Grafana 读取 CSV 文件

有没有办法在读取 csv 文件后删除它们的行? [复制]

ruby读取csv文件数据

Spark s3 csv文件读取顺序

在将 .csv 文件中的数据读取到 DataTable 中时,有没有办法强制所有列都是字符串?