gpt4 book ai didi

c# - Xamarin Forms - 如何将变量的值传递给另一个 .cs?

转载 作者:太空宇宙 更新时间:2023-11-03 22:42:06 27 4
gpt4 key购买 nike

我正在开发一个预订应用程序,我的问题是当我点击“预订”时我总是像这样得到 null JSON 值:

Picture

我认为我没有正确编写代码,这就是为什么我在尝试执行此操作时遇到问题:

Mainpage.xaml.cs

public async void btnSignin_Clicked(object sender, EventArgs e)
{
btnSignin.IsEnabled = false;
bool valid = false;
int name = 0;



HttpClient client = new HttpClient();
var response = await client.GetStringAsync("http://secret.com/potangina/final/Restserver/index.php/users/view");
var user = JsonConvert.DeserializeObject<List<Users>>(response);



for ( int i = 0; i < user.Count; i++)
{
if( (entUser.Text == user[i].mem_acc_username) && (entPass.Text == user[i].mem_acc_password))
{
valid = true;
name = i;

}
}

bool v = valid;
if (v == true)
{
await DisplayAlert("Successfully Login", "Welcome Back " + user[name].mem_fname + " " + user[name].mem_lname, "OK");


DReservation reserve = new DReservation(); // this is what i want to be passed in DineinReservation.cs i need the value of it


reserve.mem_account_no = user[name].mem_account_no;
reserve.mem_fname = user[name].mem_fname;
reserve.mem_mobile_no = user[name].mem_mobile_no;

await Navigation.PushAsync(new DineinReservation);


btnSignin.IsEnabled = true;


}
else
{
await DisplayAlert("Failed", "Invalid Username or Password", "OK");
btnSignin.IsEnabled = true;
}
}

DineinReservation.xaml.cs

然后我编写了这段代码,我认为它将用于从 Mainpage 获取 DReservation 的值

private async Task btnReserve_Clicked(object sender, EventArgs e)
{
DReservation reserve = new DReservation();

reserve.res_name = "Dine-in";
reserve.res_num_of_persons = entNumber.Text;
reserve.res_arrival_date = "test";
reserve.res_arrival_time = temp;
reserve.res_note = "none";
reserve.custom_package = "none";

var json = JsonConvert.SerializeObject(reserve);

var content = new StringContent(json, Encoding.UTF8, "application/json");

HttpClient client = new HttpClient();

var result = await client.PostAsync("http://secret.com/potangina/final/Restserver/index.php/reservation/insert_reservation", content);

if (result.StatusCode == System.Net.HttpStatusCode.Created)
{
await DisplayAlert("Success :",json, "OK");

}
else
{
await DisplayAlert("Failed", json, "OK");
}
}

DReservation.cs

public class DReservation
{

public string mem_account_no { get; set; }
public string res_name { get; set; }
public string res_num_of_persons { get; set; }
public string res_arrival_date { get; set; }
public string res_arrival_time { get; set; }
public string res_note { get; set; }
public string mem_fname { get; set; }
public string custom_package { get; set; }
public string mem_mobile_no { get; set; }
}

Users.cs

public class Users
{
public string mem_account_no { get; set; }
public string mem_no { get; set; }
public string mem_acc_username { get; set; }
public string mem_acc_password { get; set; }
public string mem_status { get; set; }
public string mem_question { get; set; }
public string mem_answer { get; set; }
public string mem_fname { get; set; }
public string mem_lname { get; set; }
public string mem_address { get; set; }
public string mem_date_applied { get; set; }
public string mem_bday { get; set; }
public string mem_mobile_no { get; set; }
}

最佳答案

页面只是 C# 类 - 您可以像使用任何 C# 类一样传递变量。即,作为构造函数的参数,或使用公共(public)属性或方法。

DReservation reserve = new DReservation(); 

reserve.mem_account_no = user[name].mem_account_no;
reserve.mem_fname = user[name].mem_fname;
reserve.mem_mobile_no = user[name].mem_mobile_no;

await Navigation.PushAsync(new DineinReservation(reserve));

然后在 DineinReservation.xaml.cs 中

public DineinReservation(DReservation reserve) {
// reserve will contain the data passed from the main page
}

关于c# - Xamarin Forms - 如何将变量的值传递给另一个 .cs?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51692389/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com