I have a custom subclass that needs to be serialized and stored in Isolated StorageSettings(wp7).I have subclass that needs to be serialized and that class extends its base class So i am serializing both classes,When serializing the classes,I am using [DataContract] to classname and [DataMember] to the variables,meanwhile i made all the variables public ,when trying to save the data in IsolatedStorageSettings i got the exception like
The data contract type XXXx is not serializable because it is not public. Making the type public will fix this error.
and so i googled and went to concept of adding assembly like
[assembly: System.Runtime.CompilerServices.InternalsVisibleTo("System.Runtime.Serialization")]
in my class files and finally data objects are getting saved,but when deactivating the app,data gets saved without error and when activating the app,i am getting the object ,but the variables are null state in the object that i restored back,I want to get the variables and its values.Am i missing anything that causes variables in my saved Object as null?
Code i am updating below
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));
}
}
}
}
and the subclass I have added below
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));
}
}
}
}
and forgot mention saving and restoring process.i am saving the mycustom Object as
var settings = IsolatedStorageSettings.ApplicationSettings;
if (settings.Contains("SimpleData"))
{
settings["SimpleData"] = requiredObj
}
else
{
settings.Add("SimpleData", requiredObj);
}
and i am restoring as gn below
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);
}
and the image of issue is below
