【问题标题】:Isolated storage exception in windows phone 7windows phone 7 中的隔离存储异常
【发布时间】:2011-07-25 14:18:12
【问题描述】:

我正在尝试做一个设置页面,用户可以在其中从 listpicker 控件中选择问题、歌曲和通过率的数量..

然后将题目、歌曲和通过率的选定索引写入隔离存储。

下面是我的代码:

int indexQues;

    string rate;
    private void saveBtn_Click(object sender, RoutedEventArgs e)
    {
        using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication()) 
        {
            if (myIsolatedStorage.FileExists("SettingFolder\\queSetting.txt")) 
            {
                myIsolatedStorage.DeleteFile("SettingFolder\\queSetting.txt");
            }

            if (myIsolatedStorage.FileExists("SettingFolder\\rateSetting.txt"))
            {
                myIsolatedStorage.DeleteFile("SettingFolder\\rateSetting.txt");
            }
        } 

        indexQues = queListPicker.SelectedIndex;
        rate = rateListPicker.SelectedItem.ToString();

        //Save the number of question to answer when the alarm ring
        //Obtain the virtual store for application
        IsolatedStorageFile myStore = IsolatedStorageFile.GetUserStoreForApplication();
        IsolatedStorageFile myStore1 = IsolatedStorageFile.GetUserStoreForApplication();
        //Create a new folder and call it "AlarmFolder"
        myStore.CreateDirectory("SettingFolder");

        //Retrieve the content of "noOfQues"
        //And write it into queSetting.txt
        StreamWriter writeFile = new StreamWriter(new IsolatedStorageFileStream("SettingFolder\\queSetting.txt", FileMode.Append, myStore));
        StreamWriter writeFile1 = new StreamWriter(new IsolatedStorageFileStream("SettingFolder\\rateSetting.txt", FileMode.Append, myStore));

        writeFile.Write(indexQues);
        writeFile1.Write(rate);
        writeFile.Close();
        writeFile1.Close();

        MessageBox.Show("Setting Saved");
        NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
    }

上面的代码允许我写入隔离存储,但是当我第三次尝试保存时出现错误。

错误是“IsolatedStorageException 未处理” 访问 IsolatedStorage 时出错。

【问题讨论】:

    标签: windows-phone-7 isolatedstorage


    【解决方案1】:

    我不确定这是否是您的问题的原因,但我从未使用 StreamWriter 写入隔离存储。尝试将编写代码更改为以下内容:

    //Save the number of question to answer when the alarm ring
    //Obtain the virtual store for application
    IsolatedStorageFile myStore = IsolatedStorageFile.GetUserStoreForApplication();
    
    //Create a new folder and call it "AlarmFolder"
    myStore.CreateDirectory("SettingFolder");
    
    //Retrieve the content of "noOfQues"
    //And write it into queSetting.txt
    using( IsolatedStorageFileStream isoStream =
      new IsolatedStorageFileStream("SettingFolder\\queSetting.txt", 
        FileMode.Append, myStore)) ) {
    
          byte[] indexBytes = UTF8Encoding.UTF8.GetBytes( indexQues.ToString() );
    
          isoStream.Write(indexBytes, 0, indexBytes.Length);
          isoStream.Flush();
    }
    
    using( IsolatedStorageFileStream isoStream =
      new IsolatedStorageFileStream("SettingFolder\\rateSetting.txt", 
        FileMode.Append, myStore)) ) {
    
          byte[] rateBytes = UTF8Encoding.UTF8.GetBytes( rate );
    
          isoStream.Write(rateBytes, 0, rateBytes.Length);
          isoStream.Flush();
    }
    

    【讨论】:

    • 嗨。我尝试将代码编辑为您的上述代码,但在“isoStrem.Write(indexQues);”上出现错误和“isoStrem.Write(rate);”错误是“方法'Write'没有重载需要1个参数。如何修改它?
    • @ben tan:抱歉,我忘了将参数转换为字节数组。 IsolatedStorageFileStream.Write 只会写一个byte[] 或一个byte。我已经编辑了答案。
    • 已更改为您的更新代码。但是第三次​​保存还是有错误。 isoStream 变为空。
    【解决方案2】:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多