通用8.1 App C#中WindowsPhone 8.1中的saveStringToLocalFile

时间:2016-05-13 14:38:02

标签: c# windows-phone-8.1 win-universal-app local-files

我有这个功能:

   public async static Task<JsonObject> GetObject(string api)
        {
            try
            {

                JsonObject jsonObject = new JsonObject();
                if (!IsInternetConnected())
                {
                    string x = await DataBase.readStringFromLocalFile(api + ".txt");
                    jsonObject = JsonObject.Parse(x);
                }
                else
                {
                    string x = "";
                    XDocument doc = XDocument.Load("DataLibrary/apis.xml");
                    var items = doc.Descendants("string");
                    foreach (XElement item in items)
                    {
                        if (item.Attribute("name").Value == api)
                        {
                            HttpClient webclient = new HttpClient();

                            HttpResponseMessage resp = webclient.PostAsync(new Uri(item.Value.ToString()), null).Result; //here
                            x = await resp.Content.ReadAsStringAsync();
                            try
                            {
                                await DataBase.saveStringToLocalFile(api + ".txt", x);
                                Debug.WriteLine("after writing");

                            }
                            catch (Exception ex)
                            {
                                throw ex;
                            }

                            Debug.WriteLine("after after writing");
                            jsonObject = JsonObject.Parse(x);
                            break;

                        }
                    }
                }
                return jsonObject;
            }
            catch (Exception)
            {
                MainPage.ShowCustomMessage(request_error_allert);
                return new JsonObject();

            }
        }

这是我从本文中获取的saveStringToFile函数:https://pumpkinszwan.wordpress.com/2014/10/27/read-and-write-text-files-windows-8-1-phone-8-1-universal-apps/

 public static async Task saveStringToLocalFile(string filename, string content)
        {
            // saves the string 'content' to a file 'filename' in the app's local storage folder
            byte[] fileBytes = System.Text.Encoding.UTF8.GetBytes(content.ToCharArray());
            Debug.WriteLine("1");

            // create a file with the given filename in the local folder; replace any existing file with the same name
            StorageFile newFile = await Windows.Storage.ApplicationData.Current.LocalFolder.CreateFileAsync(filename, CreationCollisionOption.ReplaceExisting);
            Debug.WriteLine("2");

            // write the char array created from the content string into the file
            using (var stream = await newFile.OpenStreamForWriteAsync())
            {
                await stream.WriteAsync(fileBytes, 0, fileBytes.Length);
                Debug.WriteLine("3");
            }

            Debug.WriteLine("4");
        }

我使用2种不同的API调用GetObject 2次。 当代码到达saveStringToFile函数时,它崩溃了:

  • 有时它会在saveStringTofile内崩溃 的Debug.WriteLine( “1”);

  • 有时在Debug.WriteLine(“2”);

  • 之后
  • 有时在Debug.WriteLine(“3”);

  • 之后
  • 有时在Debug.WriteLine之后(“写完后”);

  • 有时它第一次使用第一个API并崩溃 第二个。

它的行为真的很奇怪,我试图用每个断点抓住它但它每次都随机崩溃。我缺少什么?

0 个答案:

没有答案