0

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

4

1 回答 1

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 的建议处理订单。

于 2014-05-17T18:00:02.380 回答