Login Screen Error - Xamarin Forms Visual Studio 2017 Android

Asked

Viewed 151 times

0

I’m having a hard time finding the content of the Element Entry and compare to the value being stored in the BD Sqlite.

Follow the code of my class Usuarios:

public class Usuarios
{
    [PrimaryKey, AutoIncrement]
    public long? usu_id { get; set; }
    [MaxLength(20)]
    public string usu_nome { get; set; }
    [MaxLength(20)]
    public string usu_senha { get; set; }
}

Code of ContentPage TelaLogin.xaml:

<StackLayout Margin="10">
    <!--Aqui estamos inserindo o logotipo da empresa-->
    <Frame HorizontalOptions="CenterAndExpand" 
           Padding="3" Margin="3">
        <Image Source="logo.jpg" />
    </Frame>
    <Label Text="Login" 
           HorizontalTextAlignment="Center" 
           FontSize="Large" TextColor="Cyan" Style="bold"/>
    <!--Aqui estamos buscando na tabela Usuarios, o Nome e a Senha-->
    <Entry x:Name="txtUsuario" 
           Placeholder="Usuário" 
           PlaceholderColor="Cyan" 
           Text="{Binding Usuario}" 
           TextColor="Cyan" 
           Style="bold"/>
    <Entry x:Name="txtSenha" 
           Placeholder="Senha" 
           PlaceholderColor="Cyan" 
           IsPassword="True" 
           Text="{Binding Senha}" 
           TextColor="Cyan" 
           Style="bold"/>
    <Button Text="Login" 
            Clicked="btnTelaLogin" />
    <Button Text="Tela Usuários" 
            Clicked="btnTelaUsuarios" 
            BackgroundColor="Orange" TextColor="White" />
</StackLayout>

Code of Telalogin.xaml.Cs:

[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class TelaLogin : ContentPage
{
    public TelaLogin ()
    {
        InitializeComponent ();
        //remove a barra de navegação
        NavigationPage.SetHasNavigationBar(this, false);
    }

    private async void btnTelaLogin(object sender, EventArgs e)
    {
        Usuarios login = new Usuarios();
        if (login.usu_nome != null && login.usu_nome != null)
        {                
            if(login.usu_nome ==  txtUsuario.Text && login.usu_senha == txtSenha.Text)
            {
                await Navigation.PushAsync(new TelaPrincipal());
            }
            else
            {
                await DisplayAlert("Erro", "Usuário ou Senha Estão Inválidos. Verifique !!!!", "OK");
            }
        }
        else
        {
            await DisplayAlert("Erro", "Usuário ou Senha Estão Vazios. Verifique as Caixas de texto !!!!", "OK");
        }            
    }

    private async void btnTelaUsuarios(object sender, EventArgs e)
    {
        await Navigation.PushAsync(new TelaUsuarios());
    }
}

I’m not getting the test on the following line:

if(login.usu_nome ==  txtUsuario.Text && login.usu_senha == txtSenha.Text)

You don’t recognize the txtusuario.Text and the txtSenha.Text.

User registration is working.

Where can I be missing?

Grateful for an answer, if possible.

  • How did you state these names right on Xaml, should recognize. Could be some cache problem. Try to delete folders bin and obj of your project, make a clean and a rebuild. Warns if the problem persists

  • Thank you Epal help Diego. That was it, I did the cleaning and the objects were recognized. Now I am not able to compare what is typed in the text box txtusuario and txtSelf, cm the value that is saved in the database, usu_name and usu_password, where I can be missing?

  • I will post as an answer to finalize this thread. From there Oce opens a new question for us to address specifically this comparison there that mentioned. It was not very clear to me

1 answer

0

The only requirements for you to use an element defined via XAML in the codebehind are:

  • Declare ownership x:Name element with the name that will be used as variable
  • Use a unique name for each element

How did you state these names right on Xaml, should recognize, but sometimes Visual Studio and Android have some problems with caching. I’ve experienced some of these situations, but I haven’t been able to determine exactly what the causative agent is, but the solution is usually one of the options below (or a combination of them):

Visual Studio

  • Delete the folders bin and obj of the solution projects
  • Do the Clean and Rebuild of the solution
  • Uncheck Use Fast Deployment android project
  • Uncheck Use Shared Runtime android project

Android

  • Manually uninstall the previous version of the application
  • Manually uninstall all debug applications installed by visual studio (Mono Shared Runtime and Xamarin.Android API-XX Support)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.