简体   繁体   English

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

[英]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.

相关问题 Xamarin Forms:如何将媒体文件从内容页面(弹出)传递到另一个内容页面 - Xamarin Forms: How Can i Pass Media File From a content page (popup) to another content page Xamarin.Forms我如何从另一个CS文件访问公共功能? - Xamarin.Forms how do I access a public function from another CS file? Xamarin Forms不能更改变量值 - Xamarin Forms can't change variable value Xamarin表格如何将Entry值从一个类传递到另一个类List? - Xamarin forms how to pass Entry value from one to class to another class List? 如何从 Android MainActivity.cs 文件中更改 Xamarin.forms 中页面 Label 的文本? - How can I change the text of a page's Label in Xamarin.forms from the Android MainActivity.cs file? Xamarin.Forms 和 Prism - 如何传递数据并导航到另一个视图? - Xamarin.Forms and Prism - How to pass data and navigate to another view? 如何将变量从 webview Javascsript 传递到 Xamarin Forms 代码? - How to pass a variable from webview Javascsript to Xamarin Forms code? 如果我的属性是数据库中的字符串(xamarin.forms),我如何将颜色传递给我的 UI - How can i pass a color to my UI if my property is a string in my database (xamarin.forms) ASP.NET:如何将变量的值从视图传递给另一个视图 - ASP.NET: How can I pass the value of a variable from a view to another one 如何使用 c# xamarin 表单将参数传递到上一页 - how can i pass parameters to previous page using c# xamarin forms
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM