2

Im trying to persist some data but im getting an error here.
Declaration of isolated storage inside my public partial mainpage class

IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;

implementation of onNavigatedFrom

protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
    {
        settings.Add("list",listBox1.ItemsSource);
        settings.Save();
    }

When I hit the start button on the emulator I get a security exception:

System.Security.SecurityException was unhandled
Message=SecurityException

My listbox is binded to data coming from a xml. I´m using linq to xml to read it.

I have read a similar question here: SecurityException was unhandled when using isolated storage
But I could not understand what the person meant with "stored class needs to be marked public internals not allowed".
Any help would be nice. Thx!

Community
  • 1
  • 1
Diego Vin
  • 193
  • 2
  • 12

2 Answers2

1

When you save to the settings, you need to have a clear data type. In this case, you're just saving the ItemsSource, but what is actually in the items source? That data needs to be publically knowable so that the serializer can serialize it. What data is in the ListBox? How is it defined?

An IEnumerable (as such) also cannot be serialized, because the serializer needs to know what type to serialize it as.

I'd recommend code like this:

    var data = (IEnumerable<MyDataType>)listBox1.ItemsSource; // perform the cast to get the correct type;
    settings.Add("list", data.ToArray()));
    settings.Save();

This way, it's in a nice clean datatype for the serializer.

McKay
  • 12,334
  • 7
  • 53
  • 76
  • @DiegoVin Updated my answer to contain an example. – McKay Dec 01 '11 at 15:20
  • Worked perfectly! I had been trying to put this data into Isolated Storage for days and nothing worked hehe! Yesterday I found out that I could put all the ienumerable into a List, but I would have to iterate all the items again to put then into the List. Your solution is much cleaner! Now, how Do I know what collections can be serialized? The method ToArray() turns the IEnumerable into an Array? So I can presume Arrays can be serialized and IEnumerables cant? THX! – Diego Vin Dec 02 '11 at 13:03
  • Yeah, an IEnumerable can't be serialized, because there isn't a concrete type to serialize. Without going in to too much detail (which is available on MSDN, if you really want it), The serializer needs to have a concrete type with a constructor that has zero parameters. IEnumerable isn't a concrete type, it's an interface. List is a concrete type, so is Array. Arrays are really great storage mechanisms, because they are a fixed size, and have very little overhead. Lists work great if you know the collection size is going to change, but generally isn't as good for storage. – McKay Dec 05 '11 at 14:49
  • awesome! this info has helped me a lot with my problems in tombstoning collections. thx again =) – Diego Vin Dec 05 '11 at 19:39
0

What is the object collection assigned to listbox1.ItemsSource?

My guess is that it's something that can't be serialized. The SecurityException would indicate the the serialization can't be done because it's a class that's not public.
Change the accessibility of the class and ensure it can be serialized.

Matt Lacey
  • 65,560
  • 11
  • 91
  • 143
  • Linq to xml returns me an IEnumerable, so thats the collection. Im having serious problems with tombstoning this data. I´ve even tried your tombstone helper, but it did not work for this purpose... – Diego Vin Nov 30 '11 at 16:55
  • An `IEnumerable` of what? Tombstone Helper only persists static data there's too much variation in what people do with their viewmodels to have a single solution that will work for every scenario. – Matt Lacey Nov 30 '11 at 17:11