Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从file.json加载对象列表_C#_Wpf_Json_Xaml_Windows Phone 8 - Fatal编程技术网

C# 从file.json加载对象列表

C# 从file.json加载对象列表,c#,wpf,json,xaml,windows-phone-8,C#,Wpf,Json,Xaml,Windows Phone 8,我有一个Add.xaml文件 在里面,我有我的recipe类,它将我的recipe对象存储在名为breakfastRecipe的列表中 然后,我将FastRecipe列表存储在一个breakth.json文件中。(使用我在此处从另一个成员找到的反序列化/序列化方法) 我有两个文本框,我在其中写入名称+成分,然后使用一个保存按钮激活json序列化程序。我使用测试方法测试反序列化程序是否一切正常在add.xaml页面上,对象列表的保存和加载工作正常 我还有另一个页面breakfastList.xam

我有一个Add.xaml文件 在里面,我有我的recipe类,它将我的recipe对象存储在名为breakfastRecipe的列表中

然后,我将FastRecipe列表存储在一个breakth.json文件中。(使用我在此处从另一个成员找到的反序列化/序列化方法)

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

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

我使用相同的get/反序列化程序加载数据

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";
        }
    }
public异步void btnGet\u Tap()
{
StorageFolder localFolder=ApplicationData.Current.localFolder;
尝试
{
//从文件(如果存在)获取JSON,或者从文件(如果不存在)获取未找到异常
StorageFile textFile=await localFolder.GetFileAsync(“breakfastList.json”);
使用(irandomaccesstream textStream=await textFile.OpenReadAsync())
{
//读取文本流
使用(DataReader textReader=新DataReader(textStream))
{
//变大
uint textLength=(uint)textStream.Size;
等待textReader.LoadAsync(textLength);
//读一下
string jsonContents=textReader.ReadString(textLength);
//反序列化回到我们的产品!
//我只需要在这个函数中更改下面这一行
breakfastRecipe=JsonConvert.DeserializeObject(jsonContents)作为列表;
//并展示它
//显示产品();
}
}
}
捕获(例外情况除外)
{
tester.Text=“错误”;
}
}
当我在构造函数中写入

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

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


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

尝试将btnGet\u Tap代码移动到一个异步函数中,该函数返回一个任务,然后在btnGet\u 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";
        }
    }
public异步void btnGet\u Tap()
{
等待Foo();
}
公共异步任务Foo()
{
StorageFolder localFolder=ApplicationData.Current.localFolder;
尝试
{
//从文件(如果存在)获取JSON,或者从文件(如果不存在)获取未找到异常
StorageFile textFile=await localFolder.GetFileAsync(“breakfastList.json”);
使用(irandomaccesstream textStream=await textFile.OpenReadAsync())
{
//读取文本流
使用(DataReader textReader=新DataReader(textStream))
{
//变大
uint textLength=(uint)textStream.Size;
等待textReader.LoadAsync(textLength);
//读一下
string jsonContents=textReader.ReadString(textLength);
//反序列化回到我们的产品!
//我只需要在这个函数中更改下面这一行
breakfastRecipe=JsonConvert.DeserializeObject(jsonContents)作为列表;
//并展示它
//显示产品();
}
}
}
捕获(例外情况除外)
{
tester.Text=“错误”;
}
}

它仍然没有加载我的breakfastList.json文件,当我通过add.xaml页面向列表中添加一些菜谱时,我使用一个输出菜谱的按钮进行测试[0]。关闭应用程序,重新打开,再次转到add.xaml,使用my bouton进行测试,正确地从.json文件加载列表。但是当使用相同的方法btnGet_点击breakfastList.xaml内部时,它不会。当我通过add.xaml在localfolder中创建json文件时,它是否位于breakfastList.xaml之外的另一个目录中?该目录应保持不变。你有Windows Phone电动工具吗?您可以使用它查看独立存储中的文件。