-1
I have the main page and Contentpage inside the Views folder. Content is successfully loaded.
<Tab Title="Entrar In" Icon="enter.png">
<ShellContent ContentTemplate="{DataTemplate local:Entrar}" />
</Tab>
Inside this Contentpage I have a button to execute the login
<Button Text="Entrar" Clicked="DoLogin" />
Login method
public async void DoLogin(object sender, EventArgs e)
{
var UserLogin = new UserLogin
{
email = emailLogin.Text,
password = passwordLogin.Text
};
try
{
using (var httpClient = new HttpClient())
{
var dataUser = JsonConvert.SerializeObject(UserLogin);
HttpContent requestContent = new StringContent(dataUser, Encoding.UTF8, "application/json");
httpClient.DefaultRequestHeaders.Add("Locale", CultureInfo.CurrentCulture.Name);
var requestResponse = await httpClient.PostAsync("endereço", requestContent);
if (requestResponse.IsSuccessStatusCode)
{
....
}
else
{
await Application.Current.MainPage.DisplayAlert("", "Desculpe, houve uma falha na sua requisição.", "OK");
}
}
}
catch (Exception)
{
await Application.Current.MainPage.DisplayAlert("", "Desculpe, houve uma falha na sua requisição.", "OK");
}
}
The fault (I’m sure I’m missing some "link" by is starting in this language) occurs because the Login method is inside the User class that is inside the Models folder.
Display your login method
– Leandro Angelo
Before moving the login method to the Models/ User class folder, I was able to log in.
– RRV
You just moved or moved and corrected the
namespaces
? By the code presented now, you should be getting a different error message...– Leandro Angelo
I added using Myapp.Models and using Myapp.Views inside Appshell.xaml.Cs and xmlns:local="clr-namespace:Myapp.Views" inside Appshell.xaml. I have not added reference to Myapp.Models within Appshell.xaml.
– RRV
And as the button with the event
DoLogin()
will know that he needs to make this bind searching for the method inclass User
?– Leandro Angelo
This is the point, I tried to make this bind using Icommand and it didn’t work either.. I’m searching but still haven’t found how to do it.
– RRV
It is a little confused and broad, I think you will have review its structure. Incidentally the
DoLogin()
should not be in theServices
instead ofModels
?– Leandro Angelo
If I go back and put the whole structure within the same shampoo it works, but I would like to follow the good rules and also learn. I’m really going to migrate into Services right now and.. I’ll do it right now and tell you whether it worked or not.
– RRV
I created a View with the methods and made a contextpage Bind, <Contentpage.Bindingcontext> <vm:Userviewmodel /> </Contentpage.Bindingcontext>. But the Bind of the login and password fields for registration gives the error: [0:] Binding: 'Emaillogin' Property not found on 'Myapp.ViewModels.Userviewmodel', target Property: 'Xamarin.Forms.Entry.Text' . Within Entry I used Text="{Binding Passwordlogin}" and userViewModel.Cs public string Emaillogin { get; }
– RRV
Details, I had to create public Icommand Dologin {get;} and when I click the button to send execute Dologin = new Command(Login). The method to login is the public async void Login(Object Parameter)
– RRV
Solved then? It would be nice to post the answer showing how you did so you can help other users in the future.
– Leandro Angelo
I will update yes. but still have to solve the Binding problem of the login and password fields.. Today I will analyze and update the post..
– RRV