【问题标题】:Variable returning empty变量返回空
【发布时间】:2015-01-12 01:27:06
【问题描述】:

我有一个 CollectionView,在其中我有一个从某个位置读取文件的方法,一旦读取,就可以将其添加到变量中。这是我的代码:

class Cvs
{
    static string x1, x2, x3;
    public class Items
    {
        public string ItemID { get; set; }
        public string ItemTitle { get; set; }
        public string ItemBody { get; set; }
    }

    public class ItemList
    {
        List<Items> item = new List<Items>();
        public ItemList()
        {
            ReadStuff();

            item.Add(new Items() { ItemID = x1, ItemTitle = x2, ItemBody = x3 });
        }
        public List<Items> GetitemList()
        {
            return item;
        }
    }

    public async static void ReadStuff()
    {
        var AppStorage = ApplicationData.Current.LocalFolder;
        var itemFolders = await AppStorage.GetFolderAsync(@"App\Folder\");
        var Items = await itemFolders.GetFoldersAsync();
        foreach (var itemFolder in Items)
        {
            var itemTitle = await AppStorage.GetFileAsync(string.Format(@"App\Folder\{0}\Title.txt", itemFolder.Name));
            var itemBody = await AppStorage.GetFileAsync(string.Format(@"App\Folder\{0}\Body.txt", itemFolder.Name));

            var itemReadTitle = await FileIO.ReadTextAsync(itemTitle);
            var itemReadBody = await FileIO.ReadTextAsync(itemBody);

            x1 = itemFolder.Name;
            x2 = itemReadTitle;
            x3 = itemReadBody;
        }
    }
}

它应该做的是读取这些文件,当它读取这些文件时,它意味着将其添加到 x1x2x3,然后将 x1x2x3 添加到该:

item.Add(new Items() {ItemID = x1, ItemTitle = x2, ItemBody = x3});

即使文件不是空的,最终结果也是空的。我该如何解决?

【问题讨论】:

    标签: c# variables null collectionview


    【解决方案1】:

    你没有等待 ReadStuff()

    public ItemList()
    {
        Initialize();
    }
    
    public async void Initialize()
    {
        await ReadStuff();
        item.Add(new Items() { ItemID = x1, ItemTitle = x2, ItemBody = x3 });
    }
    

    您必须将 ReadStuf 从 void 更改为 Task

    【讨论】:

    • 我无法将异步添加到public async ItemList(),因为它不允许 mt 在构造函数中添加它。
    • @F4z 你应该使用 ReadStuff().Wait();在构造函数中,但这不是一个好习惯。您可以有一个空的构造函数并在对象创建后调用 Initialize。
    【解决方案2】:

    1) 你必须awaitReadStuff()

    2) 因为ReadStuff 正在迭代,所以设置静态(x 变量)将不起作用。更改 ReadStuff 以将该项目也添加到列表中。

    public async static void ReadStuff(List<Items> items)
    {
        var AppStorage = ApplicationData.Current.LocalFolder;
        var itemFolders = await AppStorage.GetFolderAsync(@"App\Folder\");
        var Items = await itemFolders.GetFoldersAsync();
        foreach (var itemFolder in Items)
        {
            var itemTitle = await AppStorage.GetFileAsync(string.Format(@"App\Folder\{0}\Title.txt", itemFolder.Name));
            var itemBody = await AppStorage.GetFileAsync(string.Format(@"App\Folder\{0}\Body.txt", itemFolder.Name));
    
            var itemReadTitle = await FileIO.ReadTextAsync(itemTitle);
            var itemReadBody = await FileIO.ReadTextAsync(itemBody);
    
            x1 = itemFolder.Name;
            x2 = itemReadTitle;
            x3 = itemReadBody;
    
           items.Add(new Items() { ItemID = x1, ItemTitle = x2, ItemBody = x3 });
        }
    }
    

    【讨论】:

    • 我无法将异步添加到public async ItemList(),因为它不允许 mt 在构造函数中添加它。当方法中只有一个构造函数专有时,我将如何在方法中添加项目:`public ItemList()
    • 刚刚得到您的更新答案。一会儿告诉你
    • 更改 ReadStuff 以接受列表作为参数。
    • 它不能正常工作。由于某些原因。它会加载项目,但不会加载所有内容。我必须重新启动应用程序,但仍然无法加载所有内容。
    • 可能是构造函数中的async 逻辑。尝试删除它,看看会发生什么。
    猜你喜欢
    • 2019-10-05
    • 2021-02-11
    • 2018-02-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-09
    • 2013-09-18
    相关资源
    最近更新 更多