简体   繁体   中英

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:

图片

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

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

And then I wrote this code that I think it will serve as to get the values of DReservation from Mainpage

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; }
}

Page are just C# classes - you pass variables the way you would with any C# class. 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

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. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM