C# C中的用户类#
我有两个使用Xamarin.Forms的页面,允许用户提供他们的数据。第一个表单要求用户输入用户名和密码。另一个页面用于输入他们的名字、地址、电子邮件等 如何基于此实现从第二页添加其他用户信息 用户类别:C# C中的用户类#,c#,class,xamarin,xamarin.forms,C#,Class,Xamarin,Xamarin.forms,我有两个使用Xamarin.Forms的页面,允许用户提供他们的数据。第一个表单要求用户输入用户名和密码。另一个页面用于输入他们的名字、地址、电子邮件等 如何基于此实现从第二页添加其他用户信息 用户类别: public class User { public string Username { get; set; } public string Password { get; set; } public string Firstname {
public class User
{
public string Username { get; set; }
public string Password { get; set; }
public string Firstname { get; set; }
public string Surname { get; set; }
public string Email { get; set; }
public string Address { get; set; }
public string Phone { get; set; }
public User() { }
public User(string Username, string Password)
{
this.Username = Username;
this.Password = Password;
}
public User(string Firstname, string Surname, string Email, string Address, string Phone)
{
this.Firstname = Firstname;
this.Surname = Surname;
this.Email = Email;
this.Address = Address;
this.Phone = Phone;
}
按下“创建”按钮时,将调用帐户创建页面中的此功能:
async void SignUserIn(object sender, EventArgs e)
{
User user = new User(entry_user.Text, entry_pass.Text);
App.IsUserLoggedIn = true;
Navigation.InsertPageBefore(new MainPage(), this);
await Navigation.PopAsync();
}
第二页-由于帐户已经存在,我不能使用“新”这个词。我怎样才能克服这个问题
public User user_details;
private async void ProceedPage_Clicked(object sender, EventArgs e)
{
user_details.Firstname = this.entryFName.Text;
user_details.Surname = this.entrySName.Text;
user_details.Phone = this.entryNumber.Text;
user_details.Email = this.entryAddress.Text;
user_details.Address = this.entryAddress.Text;
}
多谢各位
User user = new User(entry_user.Text, entry_pass.Text);
应该是
user_details = new User(entry_user.Text, entry_pass.Text);
这样,
user\u details
就可以点击ProceedPage
我的理解是,您正在尝试访问来自其他页面的变量。有很多解决方案。这里有一些。但是你可以找到比这个更合适的
1。第1页的静态变量
您始终可以从任何代码访问公共静态
变量。
考虑到你有两页:
public class Page1 : ContentPage //User input page
{
public static User user;
public Page1()
{
}
async void Clicked()
{
user = new User("username input", "password input");
}
}
public class Page1 : ContentPage //User input page
{
public Page1()
{
}
async void Clicked()
{
User user = new User("username input", "password input");
Navigation.PushAsync(new Page2(user));
}
}
第二页:
public class Page2 : ContentPage
{
public Page2()
{
//Here you can access the user variable
Page1.user.Email = "Some other input";
}
}
public class Page2 : ContentPage
{
public Page2(User user)
{
//Here you can access the user variable
user.Email = "Some other input";
}
}
这是足够好的,如果你是工作,也许一次登记表。因此,当您完成注册后,只需使用user=new user()
->创建新的空实例来清除user
2。将变量传递到下一页
您可以通过newclass
的构造函数方法传递变量。
考虑到你有两页:
public class Page1 : ContentPage //User input page
{
public static User user;
public Page1()
{
}
async void Clicked()
{
user = new User("username input", "password input");
}
}
public class Page1 : ContentPage //User input page
{
public Page1()
{
}
async void Clicked()
{
User user = new User("username input", "password input");
Navigation.PushAsync(new Page2(user));
}
}
第二页:
public class Page2 : ContentPage
{
public Page2()
{
//Here you can access the user variable
Page1.user.Email = "Some other input";
}
}
public class Page2 : ContentPage
{
public Page2(User user)
{
//Here you can access the user variable
user.Email = "Some other input";
}
}
为了获得更好的方法,如果您不使用一次性表单,可以使用模型
和视图模型
使您的UI和数据更易于管理
有几种方法可用于此目的,但我认为这是一种简单的方法。如果不想在第二页上创建另一个新用户,则需要使用公共属性更新类中的某些详细信息。将用户对象传递给第二个表单,但永远不会使用第二个包含5个字符串的构造函数