如何在 Xamarin 中的视图控制器之间传递数据

问题描述:

我试图在我的第一个和第二个 ViewController 之间传递一些数据.我已经像这样实例化了第二个视图控制器:

I am trying to pass some data between my first and second ViewController. I already instantiated the second view controller like this:

RegistrationViewController registration = new RegistrationViewController();

我将文本字段的值设置为来自注册类的变量 email,如下所示:

I set the value of the textfield to the variable email from the registration class like this:

registration.email = txtEmail.Text; 
this.NavigationController.PushViewController(registration, true);

然后在第二个 ViewController 中我像这样使用它:

Then in the second ViewController I use it like this:

public string email { get; set; }
txtEmail.Text = email ;

遗憾的是,我在 email 变量中没有值.我怎样才能成功地将数据传递给新的 ViewController.

Sadly, I don't have a value in the email variable. How can I achieve successfully passing the data to the new ViewController.

更改控制器的代码:

 registration.email = txtEmail.Text;
    string password = txtPassword.Text;
    RegistrationViewController controller = this.Storyboard.InstantiateViewController("RegistrationViewController") as RegistrationViewController;

this.NavigationController.PushViewController(registration, true);
    this.NavigationController.PushViewController(controller, true);

基于此尝试:

SecondViewController controller = this.Storyboard.InstantiateViewController("SecondViewController") as SecondViewController;
//Here you pass the data from the registerViewController to the secondViewController
controller.email = registration.email;

this.NavigationController.PushViewController(registration, true);

希望能帮到你