序列化 List<string> 时出错
Error when serialize List<string>
我正在开发一个 Windows Phone 运行时应用程序,并且我在字符串列表中有数据。但是当我暂停我的应用程序时,出现错误 Error trying to serialize the value to be written to the application data store
和 Additional information: Data of this type is not supported.
它说不支持字符串列表,有人知道我如何解决它吗?
启动时:
protected override void OnLaunched(LaunchActivatedEventArgs e)
{
if (e.PreviousExecutionState == ApplicationExecutionState.Terminated)
if (ApplicationData.Current.LocalSettings.Values.ContainsKey("lista"))
{
lista = (List<string>) (ApplicationData.Current.LocalSettings.Values["lista"]);
}
}
挂起:
private async void App_Suspending(object sender, Windows.ApplicationModel.SuspendingEventArgs e)
{
ApplicationData.Current.LocalSettings.Values["lista"] = App.lista;
}
App.lista 是在 app.xaml.cs 处声明的列表,如 public static List<string> lista = new List<string>();
您必须自己 serialize/deserialize 该列表,例如:
string Serialize(List<string> list)
{
StringBuilder result = new StringBuilder();
foreach (string s in list)
{
result.AppendFormat("{0}{1}", result.Length > 0 ? "," : "", s);
}
return result.ToString();
}
List<string> Deserialize(string s)
{
return new List<string>(s.Split(','));
}
如果您的字符串可能包含逗号,请相应地进行编码。
我正在开发一个 Windows Phone 运行时应用程序,并且我在字符串列表中有数据。但是当我暂停我的应用程序时,出现错误 Error trying to serialize the value to be written to the application data store
和 Additional information: Data of this type is not supported.
它说不支持字符串列表,有人知道我如何解决它吗?
启动时:
protected override void OnLaunched(LaunchActivatedEventArgs e)
{
if (e.PreviousExecutionState == ApplicationExecutionState.Terminated)
if (ApplicationData.Current.LocalSettings.Values.ContainsKey("lista"))
{
lista = (List<string>) (ApplicationData.Current.LocalSettings.Values["lista"]);
}
}
挂起:
private async void App_Suspending(object sender, Windows.ApplicationModel.SuspendingEventArgs e)
{
ApplicationData.Current.LocalSettings.Values["lista"] = App.lista;
}
App.lista 是在 app.xaml.cs 处声明的列表,如 public static List<string> lista = new List<string>();
您必须自己 serialize/deserialize 该列表,例如:
string Serialize(List<string> list)
{
StringBuilder result = new StringBuilder();
foreach (string s in list)
{
result.AppendFormat("{0}{1}", result.Length > 0 ? "," : "", s);
}
return result.ToString();
}
List<string> Deserialize(string s)
{
return new List<string>(s.Split(','));
}
如果您的字符串可能包含逗号,请相应地进行编码。