我有一个需要序列化并存储在独立存储设置(wp7)中的自定义子类。我有需要序列化的子类,并且该类扩展了它的基类所以我正在序列化两个类,当序列化类时,我正在使用 [ DataContract] 到类名和 [DataMember] 到变量,同时我将所有变量公开,当尝试将数据保存在 IsolatedStorageSettings 中时,我得到了如下异常
数据协定类型 XXXX 不可序列化,因为它不是公共的。公开类型将修复此错误。
所以我用谷歌搜索并进入添加组件的概念
[程序集:System.Runtime.CompilerServices.InternalsVisibleTo("System.Runtime.Serialization")]
在我的类文件中,最后数据对象被保存,但是当停用应用程序时,数据被正确保存,当激活应用程序时,我正在获取对象,但是我恢复的对象中的变量是空状态,我想要获取变量及其值。我是否遗漏了任何导致我保存的对象中的变量为空的内容?
我在下面更新的代码
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Diagnostics;
using System.Runtime.Serialization;
using System.ComponentModel;
[assembly: System.Runtime.CompilerServices.InternalsVisibleTo("System.Runtime.Serialization")]
namespace MyApp.Data
{
[DataContract]
public class MyDetail : MyDataSet,INotifyPropertyChanged
{
public MyDetail (String data)
{
ParseResponse(data);
}
public MiniStatementDetail(PhoneApplicationPage context)
{
_context = context;
}
public void performServerRequest(String mobno, String custno, String uid, String AccNo)
{
myProtocol request = this.getProtocol(4); //Pass Service ID
request.Parameters.append("mobno", mobno);
request.Parameters.append("custno", custno);
request.Parameters.append("uid", uid);
request.Parameters.append("AccNo", AccNo);
request.Submit();
}
public override void ResponseReceived(String response)
{
//
MyDetail data = new MyDetail (response);
ResponseReceived(data);
}
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged(string propName)
{
if (PropertyChanged != null)
{
PropertyChanged(
this,
new PropertyChangedEventArgs(propName));
}
}
}
}
以及我在下面添加的子类
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using MyApp.Lib;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using System.Diagnostics;
using MyApp;
using System.Xml.Serialization;//--Added for testing Purpose -Rakesh (10/4/2012)
using System.Runtime.Serialization;
using System.ComponentModel;//--Added for testing Purpose -Rakesh (10/4/2012)
using MyApp.config;
//[assembly: System.Runtime.CompilerServices.InternalsVisibleTo("System.Runtime.Serialization")]
namespace MyApp.data
{
[DataContract]//--Added for testing Purpose -Rakesh (10/4/2012)
public class MyDataSet : INotifyPropertyChanged
{
[DataMember]
public List<String[]> _data;
[DataMember]
public String[] headers;
[DataMember]
public String[] headersAr;
[DataMember]
public NameValueCollection _params;
[DataMember]
public NameValuePair _params1;
[DataMember]
public String _status;
[DataMember]
public String _refno;
[DataMember]
public String _ptoken;
[DataMember]
public String _acid;
[DataMember]
public String _branchid;
[DataMember]
public String _applicationid;
[DataMember]
public String _statusDesc;
[DataMember]
public const String HEADER_SEPARATOR = ",";
[DataMember]
public const char HEADER_SEPARATOR_SEMI = ';';
[DataMember]
public const String ROW_SEPARATOR = "@";
public event EventHandler OnRequestCompleted;
[DataMember]
public PhoneApplicationPage _context;
[DataMember]
public NameValueCollection _servicevalues;
[DataMember]
public List<String[]> parseddemo;
public List<String[]> getDataRows()
{
return _data;
}
public String[] getDataRow(int index)
{
return (String[])_data[index];
}
public String getToken() { return _ptoken; }
public String getStatus() { return _status; }
public String getRefNo() { return _refno; }
public String[] getHeaders() { return headers; }
public String getAccountID() { return _acid; }
public String getApplicationID() { return _applicationid; }
public String getStatusDesc() { return _statusDesc; }
public String getHeader(int headerConstant)
{
return headers[headerConstant];
}
public void ParseResponse(String response)
{
App.endResponse = response;
Debug.WriteLine("Parse Response >>>" + response + "::>>::");
if (_status.Equals("200"))
{
//Common.DisplayAlert(_params.getValueByName("error"));
LoggedUser.doLogout();
_context.NavigationService.Navigate(new Uri("/Design/LoginLanding.xaml", UriKind.RelativeOrAbsolute));
return;
}
_statusDesc = _params.getValueByName("StatusDesc");
_refno = _params.getValueByName("refno");
_ptoken = _params.getValueByName("ptoken");
_acid = _params.getValueByName("acid");
_branchid = _params.getValueByName("branchid");
_applicationid = _params.getValueByName("ApplicationId");
_data = ParseResponse(response, "Stmt");
}
public List<String[]> ParseResponse(String response, String param)
{
List<String[]> data = new List<String[]>();
String paramValue = _params.getValueByName(param);
if (!String.IsNullOrEmpty(paramValue))
{
String[] dataRows = paramValue.Split(ROW_SEPARATOR.ToCharArray()[0]);
for (int i = 0; i < dataRows.Length; i++)
{
data.Add(dataRows[i].Split(';'));
}
}
return data;
}
public List<String[]> getParamValueBasedData(String paramValue)
{
List<String[]> data = new List<String[]>();
if (paramValue == null) paramValue = "";
if (!String.IsNullOrEmpty(paramValue))
{
String[] dataRows = paramValue.Split(ROW_SEPARATOR.ToCharArray()[0]);
for (int i = 0; i < dataRows.Length; i++)
{
data.Add(dataRows[i].Split(';'));
}
}
return data;
}
public String[] getColumArray(int colid, String firstOption)
{
return getColumArray(_data, colid, firstOption);
}
public String[] getColumArray(int colid)
{
return getColumArray(_data, colid);
}
public String[] getColumArray(List<String[]> data, int colid)
{
String[] retValues = new String[data.Count];
for (int i = 0; i < data.Count; i++)
{
retValues[i] = ((String[])data[i])[colid];
}
return retValues;
}
public String[] getColumArray(List<String[]> data, int colid, String firstOption)
{
String[] retValues = new String[data.Count + 1];
retValues[0] = firstOption;
for (int i = 0; i < data.Count; i++)
{
retValues[i + 1] = ((String[])data[i])[colid];
}
return retValues;
}
public String getParam(String name)
{
return _params.getValueByName(name);
}
public void OnServerResponse(object sender, EventArgs e)
{
myProtocol request = (myProtocol)sender;
Debug.WriteLine("OnServerResponse::MBDataSet:" + request.Response);
ResponseReceived(request.Response);
}
public myProtocol getmyProtocol(int serviceId)
{
myProtocol request = new myProtocol(serviceId);
request.OnRequestCompleted += new EventHandler(OnServerResponse);
return request;
}
public virtual void ResponseReceived(String response)
{
}
{
if (OnRequestCompleted != null)
{
OnRequestCompleted.Invoke(data, EventArgs.Empty);
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged(string propName)
{
if (PropertyChanged != null)
{
PropertyChanged(
this,
new PropertyChangedEventArgs(propName));
}
}
}
}
忘记了保存和恢复过程。我将 mycustom 对象保存为
var settings = IsolatedStorageSettings.ApplicationSettings;
if (settings.Contains("SimpleData"))
{
settings["SimpleData"] = requiredObj
}
else
{
settings.Add("SimpleData", requiredObj);
}
我正在恢复为下面的 gn
try
{
var settings = IsolatedStorageSettings.ApplicationSettings;
MyDetail demoObj= null;
if (settings.Contains("SimpleData"))
{
Debug.WriteLine("Data Present");
settings.TryGetValue<MyDetail >("SimpleData", out demoObj)
requiredObj= demoobj;
}
else
{
Debug.WriteLine("Data Absent");
}
}
catch (Exception ex)
{
Debug.WriteLine("Exception while retreving datas" + ex);
}
问题的图片如下