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

时间:2018-08-05 07:39:29

标签: c# android json xamarin xamarin.forms

我正在开发预订应用程序,我的问题是当我点击“预订”时 我总是这样得到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; }
}

2 个答案:

答案 0 :(得分:2)

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

print(df.iloc[:,-4:])

然后在DineinReservation.xaml.cs

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

答案 1 :(得分:1)

我更喜欢使用Settings Plugin在整个应用程序中保存数据。如果需要在整个应用程序中使用某些值,则可以使用它。

我发现它比构造函数更易读和干净,并在App.cs中公开保存