简体   繁体   English

在C#中打开一个空列表

[英]Opening an empty list in c#

I'm making an app, that use one list's content, to open another list. 我正在制作一个使用一个列表内容的应用程序来打开另一个列表。 But to make the app work, it needs to open up a list from IsolatedStorage that contains nothing, the list exists, but it's empty. 但是要使该应用程序正常运行,它需要从IsolatedStorage中打开一个不包含任何内容的列表,该列表存在,但为空。

In the first MainPage.xaml.cs i create a new list, save the list to IsolatedStorage and then i pass the list's name to the other SecondPage.xaml.cs page: 在第一个MainPage.xaml.cs中,我创建一个新列表,将该列表保存到IsolatedStorage,然后将列表的名称传递给另一个SecondPage.xaml.cs页面:

String listName; //Declared global in class

IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;

List<String> emptyList = new List<String>();
settings.Add(listName, emptyList);
settings.Save();

//Here i'm passing it to the second page

NavigationService.Navigate(new Uri("/subjectsPage.xaml?key=" + listName, UriKind.Relative));

Here is the second class that handles this information and tries to open the empty list 这是处理此信息并尝试打开空白列表的第二个类

IsolatedStorageSettings settings; //Declared global in class

settings = IsolatedStorageSettings.ApplicationSettings; //Executed in public secondPage() method

Helper method: 辅助方法:

        private static object readSetting(string key)
    {
        return IsolatedStorageSettings.ApplicationSettings.Contains(key) ? IsolatedStorageSettings.ApplicationSettings[key] : null;
    }

Code to open the list itself, but it fails here, and the exception is called where the "//Failed" line is: 打开列表本身的代码,但是在这里失败,并且在“ // Failed”行所在的地方调用异常:

List List; //Declared global in class

protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        if (NavigationContext.QueryString.ContainsKey("key"))
        {
            string listName= NavigationContext.QueryString["key"];
            try
            {
                //Clear listBoxjust in case
                listBox.Clear();

                List = readSetting(listName) != null ? (List<String>)readSetting(listName) : new List<String>();
            }
            catch (Exception)
            {
              //Failed
            }
        }
    }

Do you have any ideas on how to fix this issue? 您对如何解决此问题有任何想法吗? Could i for instance add an empty string to the list, and then remove it after the list is loaded when the other page executes onNavigatedTo()??? 我可以例如向列表中添加一个空字符串,然后在其他页面执行onNavigatedTo()时在加载列表后将其删除吗?

I actually found out, that the error was that i tried to clear an un-initialized list like this: 我实际上发现了,错误是我试图清除未初始化的列表,如下所示:

            //Clear listBoxjust in case
            listBox.Clear();

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

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