2

我试图保留一些数据,但我在这里遇到错误。
在我的公共部分主页类中声明隔离存储

IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;

onNavigatedFrom 的实现

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

当我点击模拟器上的开始按钮时,我得到一个安全异常:

System.Security.SecurityException was unhandled
Message=SecurityException

我的列表框绑定到来自 xml 的数据。我正在使用 linq to xml 来阅读它。

我在这里读过一个类似的问题:SecurityException was unhandled when using isolated storage
但我不明白这个人的意思是“存储的类需要标记为不允许的公共内部”。
你能帮忙的话,我会很高兴。谢谢!

4

2 回答 2

1

当您保存到设置时,您需要有一个明确的数据类型。在这种情况下,您只是保存了 ItemsSource,但 items 源中实际存在什么?该数据需要公开可知,以便序列化程序可以对其进行序列化。ListBox 中有哪些数据?它是如何定义的?

IEnumerable(这样)也无法序列化,因为序列化程序需要知道将其序列化为什么类型。

我推荐这样的代码:

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

这样,对于序列化程序来说,它是一个非常干净的数据类型。

于 2011-11-30T16:41:41.073 回答
0

对象集合分配给listbox1.ItemsSource什么?

我的猜测是它是无法序列化的东西。SecurityException 表示无法完成序列化,因为它是一个不公开的类。
更改类的可访问性并确保它可以被序列化。

于 2011-11-30T16:45:09.260 回答