[mono-android] Deserialize from Intent.GetStringExtra issue
José M. Rivera
jmro20 at hotmail.com
Tue Nov 29 15:36:43 EST 2011
This is working for me:
********** My Class **********
[Serializable]
public class MyClass
{
#region "Global Variables"
private string mGender;
private bool mSmoker;
private DateTime mDOB;
#endregion
#region "Constructors"
public Person(string Gender, bool Smoker, DateTime DateOfBirth)
{
mGender = Gender;
mSmoker = Smoker;
mDOB = DateOfBirth;
}
public Person()
{
}
#endregion
#region "Properties"
public string Gender
{
get { return mGender; }
set { mGender = value; }
}
public bool Smoker
{
get { return mSmoker; }
set { mSmoker = value; }
}
public DateTime DOB
{
get { return mDOB; }
set { mDOB = value; }
}
#endregion
}
************** Serialization and adding the String *************
...
Bundle b = new Bundle();
MyClass myClass = new MyClass("M",false, new DateTime(1980, 01, 01));
b.PutString("myclass", Serialize(myClass));
Intent newIntent = new Intent(this, typeof(MyActivity));
newIntent.PutExtras(b);
StartActivity(newIntent);
....
public string Serialize(MyClass myClass)
{
XmlSerializer serializer = new XmlSerializer(typeof(myClass));
StringWriter writer = new
StringWriter(System.Globalization.CultureInfo.InvariantCulture);
serializer.Serialize(writer, myClass);
return writer.ToString();
}
*********** Deserialization on the new Activity ***********
private MyClass myClass;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
...
myClass = Deserialize(Intent.GetStringExtra("myclass"));
...
}
public MyClass Deserialize(string serializedData)
{
if (string.IsNullOrEmpty(serializedData))
return new MyClass;
XmlSerializer serializer = new XmlSerializer(typeof(MyClass));
StringReader reader = new StringReader(serializedData);
return (MyClass)serializer.Deserialize(reader);
}
--
View this message in context: http://mono-for-android.1047100.n5.nabble.com/Deserialize-from-Intent-GetStringExtra-issue-tp5032637p5033595.html
Sent from the Mono for Android mailing list archive at Nabble.com.
More information about the Monodroid
mailing list