Skip to content

Instantly share code, notes, and snippets.

@farukaf
Created September 17, 2020 14:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save farukaf/378dfc0775aa35323daca862d7f2cc51 to your computer and use it in GitHub Desktop.
Save farukaf/378dfc0775aa35323daca862d7f2cc51 to your computer and use it in GitHub Desktop.
Xamarin Code Behind - Login Example - With ActivityIndicator
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Shell.NavBarIsVisible="False"
Title="Login"
Visual="Material"
BackgroundColor="{StaticResource StrongBGColor}"
x:Class="LoginExample.Login">
<ContentPage.Content>
<Grid>
<StackLayout VerticalOptions="Center" Padding="30" Margin="10" Spacing="8">
<Frame HeightRequest="180" Padding="0" CornerRadius="10" IsClippedToBounds="True" HorizontalOptions="CenterAndExpand" Margin="0,0,0,50">
<Image Source="LoginLogo.png" />
</Frame>
<Label Text="Login" FontAttributes="Bold"/>
<Entry Text="" x:Name="txtLogin" />
<Label Text="Password" FontAttributes="Bold"/>
<!-- Use Password type instead -->
<Entry Text="" x:Name="txtPassword" />
<Button Text="Login" WidthRequest="200" HorizontalOptions="Center" Margin="0,30,0,10" x:Name="btnLogin" Clicked="btnLogin_Clicked" />
</StackLayout>
<Frame Padding="10"
CornerRadius="35"
Margin="10"
Opacity="0.8"
IsVisible="True"
BackgroundColor="{StaticResource StrongBGColor}"
x:Name="ActivIndicator"
VerticalOptions="StartAndExpand"
HorizontalOptions="CenterAndExpand">
<ActivityIndicator x:Name="actInd"
HeightRequest="50"
WidthRequest="50"
IsRunning="True"
IsVisible="True"
VerticalOptions="CenterAndExpand"
HorizontalOptions="CenterAndExpand"
BackgroundColor="{StaticResource LightBGColor}"
Color="{StaticResource PrimaryColor}"/>
</Frame>
</Grid>
</ContentPage.Content>
</ContentPage>
using LoginExample.Data;
using LoginExample.Services;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using Xamarin.Forms.Markup;
using Xamarin.Forms.Xaml;
namespace LoginExample
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class Login : ContentPage
{
public Login()
{
InitializeComponent();
}
protected override void OnAppearing()
{
btnLogin.IsEnabled = false;
try
{
var perfilData = new PerfilData();
var lst = perfilData.GetListAsync().Result;
if (lst != null && lst.Any())
{
//Got a Perfil/User Saved in the Sqlite/DB
var perfil = lst.First();
GlobalProperties.Perfil = perfil;
Application.Current.MainPage = new AppShell();
}
}
catch (Exception)
{
}
base.OnAppearing();
btnLogin.IsEnabled = true;
ActivIndicator.IsVisible = false;
}
private async void btnLogin_Clicked(object sender, EventArgs e)
{
//Check if the ActivIndicator is active/Show
if (ActivIndicator.IsVisible)
{
return;
}
//Make the ActivIndicator is active/Show
ActivIndicator.IsVisible = true;
string login = txtLoging.Text;
string password = txtPassword.Text;
try
{
var client = new LoginService();
var loginPayload = await client.Login(login, password);
GlobalProperties.Perfil = new Models.Perfil(loginPayload);
var perfilData = new PerfilData();
await perfilData.SaveAsync(GlobalProperties.Perfil);
//Change Main Page =>
Application.Current.MainPage = new AppShell();
}
catch (Exception ex)
{
await DisplayAlert("Erro", ex.Message, "OK");
}
finally
{
ActivIndicator.IsVisible = false;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment