问题描述
我试图保留一些数据,但我在这里遇到错误.
在我的公共部分主页类中声明隔离存储
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;
onNavigatedFrom 的实现
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
我的列表框绑定到来自 xml 的数据.我正在使用 linq to xml 来阅读它.
我在这里读过一个类似的问题:使用隔离存储时未处理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!
推荐答案
保存到设置时,需要有明确的数据类型.在这种情况下,您只是保存了 ItemsSource,但项目源中实际上是什么?该数据需要公开可知,以便序列化程序可以对其进行序列化.ListBox 中有哪些数据?它是如何定义的?
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?
一个 IEnumerable(如此)也不能被序列化,因为序列化器需要知道将它序列化为什么类型.
An IEnumerable (as such) also cannot be serialized, because the serializer needs to know what type to serialize it as.
我推荐这样的代码:
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.
这篇关于Windows Phone 上的隔离存储安全异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!