我创建了一个用户类,并像这样初始化它:
User MyUser = new User("", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "");
然后我用用户数据填充对象,然后将其传递给另一个活动,如下所示:
Intent intent = new Intent(this, typeof(MyActivity));
Bundle bundlee = new Bundle();
bundlee.PutParcelable("MyUser", MyUser); // Persist user class to next activity
intent.PutExtra("TheBundle", bundlee);
StartActivity(intent);
然后我在另一个活动中检索用户数据,如下所示:
// I initialize a variable again
User MyUser = new User("", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "");
MyUser = bundlee.GetParcelable("MyUser") as User;
我得到了除地址,城市和州以外的所有数据。很奇怪...我将断点放在代码中,并将其放在捆绑包中的所有地方,但是当我将其取出时,这三个字段是空字符串。我的用户类别是:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Java.Interop;
using Object = Java.Lang.Object;
namespace MyAndroid
{
class User : Object, IParcelable
{
private string mUsername = "";
private string mPassword = "";
private string mFirstname = "";
private string mMiddleInitial = "";
private string mLastname = "";
private string mSuffix = "";
private string mAddress = "";
private string mCity = "";
private string mState = "";
private string mZip = "";
private string mZip4 = "";
private string mHomephone = "";
private string mCellphone = "";
private string mWorkphone = "";
private string mNationalOrgn = "";
private string mCountry = "";
private string mCompany = "";
private string mDepartment = "";
private string mSection = "";
private string mProgram = "";
private string mUsertype = "";
private string mEmailaddress = "";
[ExportField("CREATOR")]
public static UserCreator InitializeCreator()
{
return new UserCreator();
}
// properties
public string Username
{
get
{
return mUsername;
}
set
{
mUsername = value;
}
}
public string Password
{
get
{
return mPassword;
}
set
{
mPassword = value;
}
}
public string Firstname
{
get
{
return mFirstname;
}
set
{
mFirstname = value;
}
}
public string MiddleInitial
{
get
{
return mMiddleInitial;
}
set
{
mMiddleInitial = value;
}
}
public string Lastname
{
get
{
return mLastname;
}
set
{
mLastname = value;
}
}
public string Suffix
{
get
{
return mSuffix;
}
set
{
mSuffix = value;
}
}
public string Address
{
get
{
return mAddress;
}
set
{
mAddress = value;
}
}
public string City
{
get
{
return mCity;
}
set
{
mCity = value;
}
}
public string State
{
get
{
return mState;
}
set
{
mState = value;
}
}
public string Zip
{
get
{
return mZip;
}
set
{
mZip = value;
}
}
public string Zip4
{
get
{
return mZip4;
}
set
{
mZip4 = value;
}
}
public string Homephone
{
get
{
return mHomephone;
}
set
{
mHomephone = value;
}
}
public string Cellphone
{
get
{
return mCellphone;
}
set
{
mCellphone = value;
}
}
public string Workphone
{
get
{
return mWorkphone;
}
set
{
mWorkphone = value;
}
}
public string NationalOrgn
{
get
{
return mNationalOrgn;
}
set
{
mNationalOrgn = value;
}
}
public string Country
{
get
{
return mCountry;
}
set
{
mCountry = value;
}
}
public string Company
{
get
{
return mCompany;
}
set
{
mACompany = value;
}
}
public string Department
{
get
{
return mDepartment;
}
set
{
mDepartment = value;
}
}
public string Section
{
get
{
return mSection;
}
set
{
mSection = value;
}
}
public string Program
{
get
{
return mProgram;
}
set
{
mProgram = value;
}
}
public string Usertype
{
get
{
return mUsertype;
}
set
{
mUsertype = value;
}
}
public string Emailaddress
{
get
{
return mEmailaddress;
}
set
{
mEmailaddress = value;
}
}
public int DescribeContents()
{
return 0;
}
public User(string Username, string Password, string Firstname, string MiddleInitial, string Lastname, string Suffix,
string address, string city, string state, string Zip, string Zip4, string Homephone, string Cellphone, string Workphone,
string NationalOrgn, string Country, string Company, string Section, string Department, string Usertype,
string Emailaddress)
{
this.mUsername = Username;
this.mPassword = Password;
this.mFirstname = Firstname;
this.mMiddleInitial = MiddleInitial;
this.mLastname = Lastname;
this.mSuffix = Suffix;
this.mAddress = Address;
this.mCity = City;
this.mState = State;
this.mZip = Zip;
this.mZip4 = Zip4;
this.mHomephone = Homephone;
this.mCellphone = Cellphone;
this.mWorkphone = Workphone;
this.mNationalOrgn = NationalOrgn;
this.mCountry = Country;
this.mCompany = Company;
this.mSection = Section;
this.mDepartment = Department;
this.mUsertype = Usertype;
this.mEmailaddress = Emailaddress;
}
public void WriteToParcel(Parcel dest, ParcelableWriteFlags flags)
{
dest.WriteString(this.Username);
dest.WriteString(this.Password);
dest.WriteString(this.Firstname);
dest.WriteString(this.MiddleInitial);
dest.WriteString(this.Lastname);
dest.WriteString(this.Suffix);
dest.WriteString(this.Address);
dest.WriteString(this.City);
dest.WriteString(this.State);
dest.WriteString(this.Zip);
dest.WriteString(this.Zip4);
dest.WriteString(this.Homephone);
dest.WriteString(this.Cellphone);
dest.WriteString(this.Workphone);
dest.WriteString(this.NationalOrgn);
dest.WriteString(this.Country);
dest.WriteString(this.Company);
dest.WriteString(this.Section);
dest.WriteString(this.Department);
dest.WriteString(this.Usertype);
dest.WriteString(this.Emailaddress);
}
}
class UserCreator : Object, IParcelableCreator
{
public Object CreateFromParcel(Parcel source)
{
return new User(source.ReadString(), source.ReadString(), source.ReadString(), source.ReadString(),
source.ReadString(), source.ReadString(), source.ReadString(), source.ReadString(), source.ReadString(),
source.ReadString(), source.ReadString(), source.ReadString(), source.ReadString(), source.ReadString(),
source.ReadString(), source.ReadString(), source.ReadString(), source.ReadString(), source.ReadString(),
source.ReadString(), source.ReadString());
}
public Object[] NewArray(int size)
{
return new Object[size];
}
}
}
我一定很想念....
答案 0 :(得分:0)
我认为您在这里有一个命名问题。看这部分
<ControlTemplate.Triggers>
<!--
The last applicable trigger wins: If both are true, GridColor will be Black
-->
<Trigger Property="HasBeenClicked" Value="True">
<Setter Property="GridColor" Value="DarkBlue"/>
</Trigger>
<Trigger Property="IsChecked" Value="True">
<Setter Property="GridColor" Value="Black"/>
</Trigger>
</ControlTemplate.Triggers>
尤其是:
public User(string Username, string Password, string Firstname, string MiddleInitial, string Lastname, string Suffix,
string address, string city, string state, string Zip, string Zip4, string Homephone, string Cellphone, string Workphone,
string NationalOrgn, string Country, string Company, string Section, string Department, string Usertype,
string Emailaddress)
{
this.mUsername = Username;
this.mPassword = Password;
this.mFirstname = Firstname;
this.mMiddleInitial = MiddleInitial;
this.mLastname = Lastname;
this.mSuffix = Suffix;
this.mAddress = Address;
this.mCity = City;
this.mState = State;
this.mZip = Zip;
this.mZip4 = Zip4;
this.mHomephone = Homephone;
this.mCellphone = Cellphone;
this.mWorkphone = Workphone;
this.mNationalOrgn = NationalOrgn;
this.mCountry = Country;
this.mCompany = Company;
this.mSection = Section;
this.mDepartment = Department;
this.mUsertype = Usertype;
this.mEmailaddress = Emailaddress;
}
正确的方法是:
this.mAddress = Address;
this.mCity = City;
this.mState = State;
实际上您没有使用参数(至少这三个参数)!这种“错误”有点意思,因为您的编译器没有抱怨。那是因为您实际上是在自己分配属性(= null),这是正确的,但肯定不是在这里;)!
将其更改为上述选项应该可以解决问题。