简体   繁体   English

从可观察集合接收数据并保存/转发

[英]Receive data from an observable collection and save / forward

Code written in C#.用 C# 编写的代码。

I have an observable collection called CartItems that keeps track of all the orders that the customer orders.我有一个名为 CartItems 的可观察集合,用于跟踪客户订购的所有订单。 For example, is there a way to read data from an observable collection and put it in a txt file?例如,有没有办法从 observable 集合中读取数据并将其放入 txt 文件中? So basically the orders that the customer orders end up in a txt file.所以基本上客户订购的订单最终都在一个txt文件中。 This txt file must then be sent to an email address.然后必须将此 txt 文件发送到电子邮件地址。 I have already created the shipping code.我已经创建了运输代码。 So the question is really how do you get data from an observable collection in a method and then put the data in a txt file?所以问题真的是你如何从一个方法中的 observable 集合中获取数据,然后将数据放入一个 txt 文件中?

//Observable collection //可观察集合
public ObservableCollection <> CartItems { get;公共 ObservableCollection <> CartItems { get; set;放; } }

You can get the items and write them in file one by one like:您可以获取项目并将它们一一写入文件,例如:

  public partial class CollectionTest : ContentPage
    {
        ObservableCollection<People> peoples = new ObservableCollection<People> {
        new People{ Name="Adam"},
        new People{ Name="Bob"},
        new People {Name="Adam2" },
        new People{ Name="Bob2"} };
        public CollectionTest()
        {
            InitializeComponent();
            mycol.ItemsSource = peoples;
        }
        private void On_ButtonClicked(object sender, EventArgs e)
        {string path= Path.Combine(FileSystem.CacheDirectory, "mylist");
            StreamWriter sw = new StreamWriter(path);
            foreach (var p in peoples)
            {sw.WriteLine(p.Name);
            }
            sw.Flush();
            sw.Close();
            Console.WriteLine(path);
                            
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM