I am trying to store objects in IsolatedStorageSettings for windows phone 7.1
the class is -
public class container
{
public int index { get; set; }
public int left { get; set; }
public int top { get; set; }
public int[] ar { get; set; }
public int count { get; set; }
public bool mark { get; set; }
public int num_e { get; set; }
public int o { get; set; }
public bool mine { get; set; }
// some functions
}
after adding the object to isolated storage, when the .save() statement is executed, the following error is generated
"An exception of type 'System.Runtime.Serialization.InvalidDataContractException' occurred in System.Runtime.Serialization.ni.dll but was not handled in user code"
Can you please tell how make it work.
Any help in this regard would be highly appreciated.
edit :: This is how I am storing.
private void save_Click(object sender, RoutedEventArgs e)
{
if (save_g.Contains("n"))
{
save_g["n"] = cons.n; //cons is a class and n is a static int
}
else
save_g.Add("n", cons.n);
if (save_g.Contains("n_boxes"))
{
save_g["n_boxes"] = cons.n_boxes; //n_boxes is a static int
}
else
save_g.Add("n_boxes", cons.n_boxes);
save_g.Save();
string t = "";
container c; //class definition as above
for (int i = 0; i < cons.n; i++)
{
t = Convert.ToString(i);
c=new container(edge[i]); //edge is an object of other class
if (save_g.Contains(t))
{
save_g[t] = c;
}
else
save_g.Add(t, c);
save_g.Save(); ****Here error occurs****
}
for (int i = 0; i < cons.n_boxes; i++)
{
t = Convert.ToString(i + cons.n);
c = new container(brick[i]); //brick is an object of other class
if (save_g.Contains(t))
{
save_g[t] = c;
}
else
save_g.Add(t, c);
save_g.Save();
}
}
Put a [DataContract] attribute on the class and [DataMember] attribute on the properties you want serialized. Normally serialization should work without these but I suspect you have some types in the class that cannot be serialized and you are not showing us these. Also make sure you have a public constructor with no arguments (or have no constructors).
P.S. in the .NET convention properties are PascalCase and names of the classes are PascalCase as well.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With