Login Screen Error - Xamarin Forms Visual Studio 2017 Android

0

I'm having difficulty fetching the contents of the Entry Element and comparing it with the value being stored in the SQLite DB.

Here is the code for 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>

DisplayLogin.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 able to test in the following line:

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

You are not recognizing txtusuario.Text and txtSenha.Text .

User registration is working.

Where can I be wrong?

Thank you for a response, if possible.

    
asked by anonymous 03.05.2018 / 01:36

1 answer

0

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

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

Since you declared these names correctly in Xaml , you should recognize them, but sometimes Visual Studio and Android have some problems with caching. I have experienced some of these situations, but I have not been able to determine exactly which is the causative agent, but the solution is usually one of the following (or the combination of them):

Visual Studio

  • Delete the bin and obj folders of the solution projects
  • Make the Clean and Rebuild of the solution
  • Uncheck the Use Fast Deployment option of the android project
  • Uncheck the Use Shared Runtime option of the 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)
06.05.2018 / 00:37