Isolated Storage security exception on windows pho

2019-05-25 23:13发布

问题:

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!

回答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.



回答2:

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.