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

时间:2014-05-16 21:57:35

标签: c# windows-phone-8 binding listpicker isolatedstoragefile

当应用程序首次运行时,我将一些默认值写入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?

1 个答案:

答案 0 :(得分:0)

感谢@ 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的建议,我会按顺序进行排序。