Hello,
In part of my code, I successively call two routes of an API ("connection login password" then "user information retrieval"). I want to display the answers for each of the two routes on the same page on the screen. The second route uses the response from the first route as an argument.
My code below works but it is not correct: The call to the second route clears the responses displayed in the labels of the first route and if a property has the same name on both instances, the display will be incorrect...
page_gestion_webservice.xaml
<StackLayout Margin="20,35,20,20">
<StackLayout>
<Entry x:Name="login_login_st"
Text="test@test.com"
Placeholder="email" />
<Entry x:Name="login_password_st"
Text="test"
Placeholder="mot de passe" />
<Button Text="to connect"
Clicked="call_api_login" />
<Label>
<Label.FormattedText><FormattedString>
<Span Text="success : " /><Span Text="{Binding login_user_success_bo}" />
</FormattedString></Label.FormattedText>
</Label>
<Label>
<Label.FormattedText><FormattedString>
<Span Text="error : " /><Span Text="{Binding login_user_error_st}" />
</FormattedString></Label.FormattedText>
</Label>
<Label>
<Label.FormattedText><FormattedString>
<Span Text="token : " /><Span Text="{Binding login_user_token_st}" />
</FormattedString></Label.FormattedText>
</Label>
<Label>
<Label.FormattedText><FormattedString>
<Span Text="userid : " /><Span Text="{Binding login_user_userid_i}" />
</FormattedString></Label.FormattedText>
</Label>
</StackLayout>
<StackLayout>
<Button Text="recup user info"
Clicked="call_api_userid" />
<Label>
<Label.FormattedText><FormattedString>
<Span Text="success : " /><Span Text="{Binding userid_success_bo}" />
</FormattedString></Label.FormattedText>
</Label>
<Label>
<Label.FormattedText><FormattedString>
<Span Text="error : " /><Span Text="{Binding userid_error_st}" />
</FormattedString></Label.FormattedText>
</Label>
<Label>
<Label.FormattedText><FormattedString>
<Span Text="prenom : " /><Span Text="{Binding userid_firstname_st}" />
</FormattedString></Label.FormattedText>
</Label>
<Label>
<Label.FormattedText><FormattedString>
<Span Text="nom : " /><Span Text="{Binding userid_name_st}" />
</FormattedString></Label.FormattedText>
</Label>
<Label>
<Label.FormattedText><FormattedString>
<Span Text="genre : " /><Span Text="{Binding userid_genre_i}" />
</FormattedString></Label.FormattedText>
</Label>
</StackLayout>
</StackLayout>
page_gestion_webservice.xaml.cs
public partial class page_gestion_webservice : ContentPage
{
Response_login_user_cl response_login_user_obj;
Response_userid_cl response_userid_obj;
public page_gestion_webservice()
{
InitializeComponent();
}
async void call_api_login(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(login_login_st.Text) || string.IsNullOrWhiteSpace(login_login_st.Text))
await DisplayAlert("Validation", "Please enter your email.", "OK");
else if (string.IsNullOrEmpty(login_password_st.Text) || string.IsNullOrWhiteSpace(login_password_st.Text))
await DisplayAlert("Validation", "Please enter your password.", "OK");
else
{
Login_param_body_cl login_param_body_obj = new Login_param_body_cl(login_login_st.Text, login_password_st.Text);
Request_api_cl request_api_login_obj = new Request_api_cl("login", Api_lapi_cl.tokengroupe_st);
response_login_user_obj = await request_api_login_obj.request_post_login_async(Api_lapi_cl.url_base_st, Api_lapi_cl.post_login_st, login_param_body_obj);
BindingContext = response_login_user_obj;
}
}
async void call_api_userid(object sender, EventArgs e)
{
if ((!string.IsNullOrEmpty(response_login_user_obj.login_user_token_st)) && (response_login_user_obj.login_user_userid_i != 0))
{
Request_api_cl request_api_userid_obj = new Request_api_cl("userid", response_login_user_obj.login_user_token_st, response_login_user_obj.login_user_userid_i);
response_userid_obj = await request_api_userid_obj.request_get_userid_async(Api_lapi_cl.url_base_st, Api_lapi_cl.get_userid_st);
BindingContext = response_userid_obj;
}
}
}
Api_lapi_cl.cs
public class Reponse_login_user_cl
{
[JsonProperty("success")]
public bool login_user_success_bo { get; set; }
[JsonProperty("error")]
public string login_user_error_st { get; set; }
[JsonProperty("token")]
public string login_user_token_st { get; set; }
[JsonProperty("user_id")]
public int login_user_userid_i { get; set; }
}
// réponse de la route user_id
public class Reponse_userid_cl
{
[JsonProperty("success")]
public bool userid_success_bo { get; set; }
[JsonProperty("error")]
public string userid_error_st { get; set; }
[JsonProperty("prenom")]
public string userid_prenom_st { get; set; }
[JsonProperty("nom")]
public string userid_nom_st { get; set; }
[JsonProperty("genre")]
// int? : rend le int nullable
public int? userid_genre_i { get; set; }
}
I am looking for a solution of this type {Binding response_login_user_obj.login_user_success_bo}
and BindingContext = this;
but my syntax is of course incorrect :
<StackLayout Margin="20,35,20,20">
<StackLayout>
[...]
<Button Text="to connect"
Clicked="call_api_login" />
<Label>
<Label.FormattedText><FormattedString>
<Span Text="success : " /><Span Text="{Binding response_login_user_obj.login_user_success_bo}" />
</FormattedString></Label.FormattedText>
</Label>
[...]
</StackLayout>
<StackLayout>
<Button Text="recup user info"
Clicked="call_api_userid" />
<Label>
<Label.FormattedText><FormattedString>
<Span Text="success : " /><Span Text="{Binding response_userid_obj.userid_success_bo}" />
</FormattedString></Label.FormattedText>
</Label>
[...]
</StackLayout>
</StackLayout>
async void call_api_login(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(login_login_st.Text) || string.IsNullOrWhiteSpace(login_login_st.Text))
await DisplayAlert("Validation", "Please enter your email.", "OK");
else if (string.IsNullOrEmpty(login_password_st.Text) || string.IsNullOrWhiteSpace(login_password_st.Text))
await DisplayAlert("Validation", "Please enter your password.", "OK");
else
{
Login_param_body_cl login_param_body_obj = new Login_param_body_cl(login_login_st.Text, login_password_st.Text);
Request_api_cl request_api_login_obj = new Request_api_cl("login", Api_lapi_cl.tokengroupe_st);
response_login_user_obj = await request_api_login_obj.request_post_login_async(Api_lapi_cl.url_base_st, Api_lapi_cl.post_login_st, login_param_body_obj);
BindingContext = this;
}
}
async void call_api_userid(object sender, EventArgs e)
{
if ((!string.IsNullOrEmpty(response_login_user_obj.login_user_token_st)) && (response_login_user_obj.login_user_userid_i != 0))
{
Request_api_cl request_api_userid_obj = new Request_api_cl("userid", response_login_user_obj.login_user_token_st, response_login_user_obj.login_user_userid_i);
response_userid_obj = await request_api_userid_obj.request_get_userid_async(Api_lapi_cl.url_base_st, Api_lapi_cl.get_userid_st);
BindingContext = this;
}
}
Can you guide me? thank you so much.