【问题标题】:Load object list from file.json从 file.json 加载对象列表
【发布时间】:2013-11-13 16:34:49
【问题描述】:

我有一个文件 Add.xaml 在里面,我有我的食谱类,将我的食谱对象存储在一个名为早餐食谱的列表中

然后我将列表 beakfastRecipe 存储在一个早餐.json 文件中。 (使用我从另一个成员这里找到的反序列化/序列化方法)

我有几个文本框,我在其中写入名称 + 成分,然后我使用一个保存按钮来激活 json 序列化程序。如果一切正常,我使用测试方法来测试反序列化器。 在 add.xaml 页面上,对象列表的保存和加载工作正常

我有另一个页面 BreakfastList.xaml,我想在其中使用 longlistselector 用我的所有食谱填充我的页面。问题是我不知道如何访问在另一页上创建的那个早餐.json 文件。

我使用相同的 get/deserializer 来加载我的数据

public async void btnGet_Tap()
    {

        StorageFolder localFolder = ApplicationData.Current.LocalFolder;
        try
        {
            // Getting JSON from file if it exists, or file not found exception if it does not
            StorageFile textFile = await localFolder.GetFileAsync("breakfastList.json");

            using (IRandomAccessStream textStream = await textFile.OpenReadAsync())
            {
                // Read text stream 
                using (DataReader textReader = new DataReader(textStream))
                {
                    //get size
                    uint textLength = (uint)textStream.Size;
                    await textReader.LoadAsync(textLength);
                    // read it
                    string jsonContents = textReader.ReadString(textLength);
                    // deserialize back to our products!
                    //I only had to change this following line in this function
                    breakfastRecipe = JsonConvert.DeserializeObject<IList<Recipe>>(jsonContents) as List<Recipe>;
                    // and show it
                    //displayProduct();
                }
            }
        }
        catch (Exception ex)
        {
            tester.Text = "error";
        }
    }

当我写在构造函数中时

早餐食谱 = 新列表(); btnGet_Tap();

在我看来,我正在创建一个新列表,然后 btnGet 从我的 add.xaml 创建的那个 .json 文件中加载数据,但它不起作用......

然后我的问题是如何访问我的“第二页”上的数据,这些数据存储在由我的第一页创建的那个 .json 文件中

【问题讨论】:

    标签: c# wpf json xaml windows-phone-8


    【解决方案1】:

    尝试将您的 btnGet_Tap 代码移动到返回任务的异步函数中,然后在 btnGet_Tap() 方法中等待。然后你可以调用

    Foo().Wait();
    

    如果您希望在调用构造函数时等待。

        public async void btnGet_Tap()
        {
            await Foo();
        }
    
        public async Task Foo()
        {
            StorageFolder localFolder = ApplicationData.Current.LocalFolder;
            try
            {
                // Getting JSON from file if it exists, or file not found exception if it does not
                StorageFile textFile = await localFolder.GetFileAsync("breakfastList.json");
    
                using (IRandomAccessStream textStream = await textFile.OpenReadAsync())
                {
                    // Read text stream 
                    using (DataReader textReader = new DataReader(textStream))
                    {
                        //get size
                        uint textLength = (uint)textStream.Size;
                        await textReader.LoadAsync(textLength);
                        // read it
                        string jsonContents = textReader.ReadString(textLength);
                        // deserialize back to our products!
                        //I only had to change this following line in this function
                        breakfastRecipe = JsonConvert.DeserializeObject<IList<Recipe>>(jsonContents) as List<Recipe>;
                        // and show it
                        //displayProduct();
                    }
                }
            }
            catch (Exception ex)
            {
                tester.Text = "error";
            }
        }
    

    【讨论】:

    • 它仍然没有加载我的早餐列表.json 文件,当我通过 add.xaml 页面将一些食谱添加到列表中时,我使用输出我的食谱 [0] 的按钮进行测试。关闭应用程序,重新打开再次转到该 add.xaml,使用我的 bouton 进行测试,从 .json 文件正确加载列表。但是当在早餐列表.xaml 中使用相同的方法 btnGet_Tap 时,它不会。当我通过 add.xaml 在 localfolder 中创建一个 json 文件时,它是不是在早餐列表.xaml 之外的另一个目录中?
    • 目录应该保持不变。你有 Windows Phone Power Tools 吗?您可以使用它来查看隔离存储中的文件。 wptools.codeplex.com
    猜你喜欢
    • 1970-01-01
    • 2014-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-16
    相关资源
    最近更新 更多