C# 使用IsolatedStorage文件中的条目填充ListPicker

C# 使用IsolatedStorage文件中的条目填充ListPicker,c#,windows-phone-8,binding,listpicker,isolatedstoragefile,C#,Windows Phone 8,Binding,Listpicker,Isolatedstoragefile,当应用程序首次运行时,我会在IsolatedStorageFile中为文本文件写入一些默认值,如下所示: if (!settings.Contains("firstrun")) { StringBuilder sb = new StringBuilder(); // Use a StringBuilder to construct output.

当应用程序首次运行时,我会在IsolatedStorageFile中为文本文件写入一些默认值,如下所示:

if (!settings.Contains("firstrun"))
            {
                StringBuilder sb = new StringBuilder();                                 // Use a StringBuilder to construct output.
                var store = IsolatedStorageFile.GetUserStoreForApplication();           // Create a store
                store.CreateDirectory("testLocations");                                 // Create a directory
                IsolatedStorageFileStream rootFile = store.CreateFile("locations.txt"); // Create a file in the root.
                rootFile.Close();                                                       // Close File
                string[] filesInTheRoot = store.GetFileNames();                         // Store all files names in an array
                Debug.WriteLine(filesInTheRoot[0]);                                     // Show first file name retrieved (only one stored at the moment)

                string filePath = "locations.txt";

                if (store.FileExists(filePath)) {

                    Debug.WriteLine("Files Exists"); 
                    StreamWriter sw =
                            new StreamWriter(store.OpenFile(filePath,
                                FileMode.Open, FileAccess.Write));

                            Debug.WriteLine("Writing..."); 
                            sw.WriteLine("Chicago, IL");
                            sw.WriteLine("Chicago, IL (Q)");
                            sw.WriteLine("Dulles, VA");
                            sw.WriteLine("Dulles, VA (Q)");
                            sw.WriteLine("London, UK");
                            sw.WriteLine("London, UK (Q)");
                            sw.WriteLine("San Jose, CA");
                            sw.WriteLine("San Jose, CA (Q)");
                            Debug.WriteLine("Writing complete"); 

                        }
                }
这似乎是可行的,但现在我需要用这个文本文件的内容按字母顺序填充我的ListPicker


最好的方法是什么?从文本文件创建一个列表,然后用列表填充ListPicker?

感谢@har07确认了我的想法。我从如下按钮启动全屏ListPicker:

private void locChoice(object sender, RoutedEventArgs e)
        {
            IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication();
            string filePath = "locations.txt";

            if (store.FileExists(filePath))
            { 
               Debug.WriteLine("Files Exists");
                try
                {
                    string fileData;
                    using (IsolatedStorageFileStream isoStream =
                        new IsolatedStorageFileStream("locations.txt", FileMode.Open, store))
                    {
                        using (StreamReader reader = new StreamReader(isoStream))
                        {
                            fileData = reader.ReadToEnd();
                        }
                    }
                    testLocationPicker.ItemsSource = fileData.Split(';');
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }

            }

            testLocationPicker.Open();
        }

我将按照@Pantelis的建议进行排序。

我建议您使用列表使用XmlSerializer进行读/写。“最好的方法是什么?从文本文件创建一个列表,然后用该列表填充ListPicker?”是的,就是这样@Mkrtich OP不是在谈论XML,它是纯文本文件。感谢har07,我一输入它就走了这条路,并设法让它工作。然而,让它按字母顺序列出条目可能更棘手。没有什么棘手的。填充列表后,只需使用
.OrderBy()
扩展方法。谢谢@Pantelis,我今天稍后会尝试。谢谢你的帮助