【问题标题】:Is there a way to populate an ObservableCollection in Xamarin.Forms by reading a csv file?有没有办法通过读取 csv 文件来填充 Xamarin.Forms 中的 ObservableCollection?
【发布时间】: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"));
}
        
}

【问题讨论】:

标签: c# csv xamarin xamarin.forms spreadsheet


【解决方案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文件中读取数据。

【讨论】:

  • 非常感谢您的帮助
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-25
  • 1970-01-01
  • 1970-01-01
  • 2018-06-10
相关资源
最近更新 更多