[英]Xamarin Forms - How can i pass the value of a variable to a another .cs?
I'm developing a reservation app , my problem is when I tapped "reserve" I always get null
JSON value just like this: 我正在开发一个预订应用程序,我的问题是当我点击“保留”时,我总是得到null
JSON值,就像这样:
I don't think I wrote the code correctly that's why I have a problem I tried to do this things: 我不认为我正确编写了代码,这就是为什么我有一个问题,我试图做这件事:
Mainpage.xaml.cs 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 DineinReservation.xaml.cs
And then I wrote this code that I think it will serve as to get the values of DReservation from Mainpage 然后我编写了这段代码,我认为它将用于从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 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 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; }
}
Page are just C# classes - you pass variables the way you would with any C# class. Page只是C#类 - 您可以像使用任何C#类一样传递变量。 Namely, as an argument to the constructor, or using a public property or method. 即,作为构造函数的参数,或使用公共属性或方法。
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));
then in DineinReservation.xaml.cs 然后在DineinReservation.xaml.cs中
public DineinReservation(DReservation reserve) {
// reserve will contain the data passed from the main page
}
I prefer using Settings Plugin for saving data across the app. 我更喜欢使用Settings Plugin在整个应用程序中保存数据。 If some values need to be used across the app I use this. 如果需要在整个应用程序中使用某些值,我会使用它。
I find it very readable and clean than Constructors and Saving publically in App.cs 我发现它比Constructors和在App.cs中公共保存更易读和干净
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.