开发者

silverlight, Save load, IsolatedStorageFile and IsolatedStorageFileStream. Exceptions

Windows Phone 7 开发者_如何学GoApp The Goal of the application is a simple To Do list. I have a class 'toditem' i add those objects to the Items object.

it seems to me I'm doing something really complicated and most likely no clean or decent code

But i have some serious problems with "IsolatedStorageFile"

 public class ToDoItem
    {
        public string ToDoName { get; set; } // Add controle's enz.
        public string ToDoDescription { get; set; }
        internal Priority PriortiySelection { get; set; }
...
}

Items class (basicly a wrapper clas so i can acces it)

public class Items
    {
        public static List<ToDoItem> Itemslist = new List<ToDoItem>();
        public static List<ToDoItem> GetList()

        static methods here..
   }

The code Belows returns the following exceptions :

"Attempt to access the method failed: System.Io.streamreader..ctor (System.String)"

and afterwards i get

Operation not permitted on IsolatedStorageFileSTream

  if (store.FileExists(@"items.std"))
                {

                    ToDoItem item = new ToDoItem();
                    try
                    {
                        IsolatedStorageFileStream save = new IsolatedStorageFileStream(@"items.std", FileMode.Open, store);
                        BinaryReader reader = new BinaryReader(save);
                    }
                    catch (Exception exc)
                    {
                        MessageBox.Show(exc.Message);
                    }

in public partial class NewToDo : PhoneApplicationPage i added the following method. wich returns the above exceptions again i only assume that its allowd for some reason or i make some huge mistakes.

 private void saveItem(ToDoItem toDoItem)
        {
            try
            {
                using (StreamWriter sw = new StreamWriter(store.OpenFile(@"items.std", FileMode.Append)))
                {
                    sw.WriteLine(toDoItem.ToDoName);
                    sw.WriteLine(toDoItem.ToDoDescription);
                    sw.WriteLine(toDoItem.PriortiySelection.ToString());
                }
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }

        }

Should u need more information I'm always glad to provide it, I'm currently a student at a Belgium college second year and I'm playing around with windows phone7 apps.


The following will read the contents of a file from isolated storage

using (var store = IsolatedStorageFile.GetUserStoreForApplication())
{
    if (!store.FileExists(VIEW_MODEL_STORAGE_FILE))
    {
        return result;
    }

    using (var isfs = new IsolatedStorageFileStream(VIEW_MODEL_STORAGE_FILE, FileMode.Open, store))
    {
        using (var sr = new StreamReader(isfs))
        {
            string lineOfData;

            while ((lineOfData = sr.ReadLine()) != null)
            {
                result += lineOfData;
            }
        }
    }
}

The example builds a string of data (result). This is actually a serialized object which is actually a collection of other objects. This can then be deserialized back to the collection. This is probably preferable to what you were trying to do with writing out properties to a file one at a time.

Here's how to write the file:

using (var store = IsolatedStorageFile.GetUserStoreForApplication())
{
    using (var isfs = new IsolatedStorageFileStream(VIEW_MODEL_STORAGE_FILE, FileMode.Create, store))
    {
        using (var sw = new StreamWriter(isfs))
        {
            sw.Write(serializedCollectionObject);
            sw.Close();
        }
    }
}


Is it possible you're not disposing all your disposable objects and encountering a problem when you try to access a resource for a second time because it's still in use?

The using statement is a good way to handle this easily, more on that here.

Dispose with Using

A bit more background on the topic here where Jm47 was getting the same error message for this reason.

Problem opening a stream to an isolatedstorage image already the source on an image?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜