简体   繁体   English

如何使用从 .txt 文件中读取的对象填充我的列表? (C# UWP)

[英]How can I populate back my list with objects read from .txt file? (C# UWP)

I'm not familiar with FileIO in UWP apps, but I managed to save my list to a .txt file with a method I found on this site.我不熟悉 UWP 应用程序中的 FileIO,但我设法使用我在此站点上找到的方法将我的列表保存到 .txt 文件。 The thing is, I have no idea how could I read the file and populate my list with those objects.问题是,我不知道如何读取文件并用这些对象填充我的列表。 My object class looks like this:我的对象类如下所示:

public class Item : ObservableObjectBase
    {
        private string id;
        public string ID
        {
            get => id;
            set => SetAndNotifyIfChanged(ref id, value);
        }

        private string description;
        public string Description
        {
            get => description;
            set => SetAndNotifyIfChanged(ref description, value);
        }

        private int quantity;
        public int Quantity
        {
            get => quantity;
            set => SetAndNotifyIfChanged(ref quantity, value);
        }

        private Category category;
        public Category Category
        {
            get => category;
            set => SetAndNotifyIfChanged(ref this.category, value);
        }

        private int price;
        public int Price
        {
            get => price;
            set => SetAndNotifyIfChanged(ref price, value);
        }

where Category is:其中类别是:

public class Category : ObservableObjectBase
    {
        private string name;
        public string Name
        {
            get => name;
            set => SetAndNotifyIfChanged(ref name, value);
        }

        private bool isSensitive;
        public bool IsSensitive
        {
            get => isSensitive;
            set => SetAndNotifyIfChanged(ref isSensitive, value);
        }

I managed to save these Item objects to a file like this:我设法将这些Item对象保存到这样的文件中:

private async void Button_Click_2(object sender, RoutedEventArgs e)
        {
            var sortedLines = itemlist.OrderBy(i => i.ID)
                          .Select(i => $"{i.ID}*{i.Category.ToString()}*{i.Description}*{i.Price.ToString()}*{i.Quantity.ToString()}");

            StorageFolder folder = ApplicationData.Current.LocalFolder;
            StorageFile file = await folder.CreateFileAsync("data.txt", CreationCollisionOption.ReplaceExisting);

            if (file != null)
            {
                await FileIO.WriteLinesAsync(file, sortedLines);
            }
        }

Here I save Category as a string, its overriden ToString method looks like this:在这里,我将 Category 保存为字符串,其覆盖的ToString方法如下所示:

private string IsSensitiveAsString => IsSensitive ? "[S]" : "";
public override string ToString() => $"{Name} {IsSensitiveAsString}";

In the text file I seperated the properties with *, since the description can be long and can contain commas, spaces and what not.在文本文件中,我用 * 分隔了属性,因为描述可能很长,并且可以包含逗号、空格等等。 After adding 2 items, this is how they appear in the text file:添加 2 个项目后,这就是它们在文本文件中的显示方式:

ABC123*SUV *Item1's description. Contains spaces! *3000*40
DEF456*OldTimer [S]*Item2's description. This one can be really long too!!*5400*82

I'm totally lost about the method I could do this considering it's a UWP app, I could do a single string, but I don't have any idea how could I convert back my Category to its Name and IsSensitive property.考虑到它是一个 UWP 应用程序,我完全迷失了我可以执行此操作的方法,我可以执行单个字符串,但我不知道如何将我的 Category 转换回其 Name 和 IsSensitive 属性。 Also if the item's description is too long, it can be multiple lines long in the text file and I'm not sure if that would cause some problems.此外,如果项目的描述太长,文本文件中可能会有多行,我不确定这是否会导致一些问题。 Any help would be greatly appreciated!任何帮助将不胜感激!

How can I populate back my list with objects read from .txt file?如何使用从 .txt 文件中读取的对象填充我的列表? (C# UWP) (C# UWP)

As @Michał Turczyn said, the better way is serialization the list to JSON string, and retrieve the list object with JSON deserialization.正如@Michał Turczyn 所说,更好的方法是将列表序列化为 JSON 字符串,并使用 JSON 反序列化检索列表对象。

For Example.例如。

serialization连载

 var items = new ObservableCollection<string>() { "hee", "ss", "dasde" };
 string output = JsonConvert.SerializeObject(items);

deserialization反序列化

 string json = await Windows.Storage.FileIO.ReadTextAsync(file);
 var items = JsonConvert.DeserializeObject<ObservableCollection<string>>(json);

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

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