Skip to content

Instantly share code, notes, and snippets.

@Axemasta
Created May 24, 2021 14:13
Show Gist options
  • Select an option

  • Save Axemasta/6c81601b130932e963925c45d68cff6c to your computer and use it in GitHub Desktop.

Select an option

Save Axemasta/6c81601b130932e963925c45d68cff6c to your computer and use it in GitHub Desktop.
Update Profile After Login
using System;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
using ShellDemo.Services;
using ShellDemo.Views;
namespace ShellDemo
{
public partial class App : Application
{
public static string CurrentUserName { get; set; }
public App()
{
InitializeComponent();
DependencyService.Register<MockDataStore>();
MainPage = new AppShell();
}
protected override void OnStart()
{
}
protected override void OnSleep()
{
}
protected override void OnResume()
{
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<Shell xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:ShellDemo.Views"
Title="ShellDemo"
x:Class="ShellDemo.AppShell">
<!--
The overall app visual hierarchy is defined here, along with navigation.
https://docs.microsoft.com/xamarin/xamarin-forms/app-fundamentals/shell/
-->
<Shell.Resources>
<ResourceDictionary>
<Style x:Key="BaseStyle" TargetType="Element">
<Setter Property="Shell.BackgroundColor" Value="{StaticResource Primary}" />
<Setter Property="Shell.ForegroundColor" Value="White" />
<Setter Property="Shell.TitleColor" Value="White" />
<Setter Property="Shell.DisabledColor" Value="#B4FFFFFF" />
<Setter Property="Shell.UnselectedColor" Value="#95FFFFFF" />
<Setter Property="Shell.TabBarBackgroundColor" Value="{StaticResource Primary}" />
<Setter Property="Shell.TabBarForegroundColor" Value="White"/>
<Setter Property="Shell.TabBarUnselectedColor" Value="#95FFFFFF"/>
<Setter Property="Shell.TabBarTitleColor" Value="White"/>
</Style>
<Style TargetType="TabBar" BasedOn="{StaticResource BaseStyle}" />
<Style TargetType="FlyoutItem" BasedOn="{StaticResource BaseStyle}" />
</ResourceDictionary>
</Shell.Resources>
<FlyoutItem Title="Feed"
FlyoutDisplayOptions="AsMultipleItems">
<Tab Title="About">
<ShellContent Title="About" Icon="icon_about.png" Route="AboutPage" ContentTemplate="{DataTemplate local:AboutPage}" />
</Tab>
<Tab Title="Browse">
<ShellContent Title="Browse" Icon="icon_feed.png" ContentTemplate="{DataTemplate local:ItemsPage}" />
</Tab>
<Tab Title="Profile">
<ShellContent Title="Profile" Icon="icon_feed.png" ContentTemplate="{DataTemplate local:ProfilePage}" />
</Tab>
</FlyoutItem>
<!--<TabBar>
<ShellContent Title="About" Icon="icon_about.png" Route="AboutPage" ContentTemplate="{DataTemplate local:AboutPage}" />
<ShellContent Title="Browse" Icon="icon_feed.png" ContentTemplate="{DataTemplate local:ItemsPage}" />
</TabBar>
<TabBar>
<ShellContent Route="LoginPage" ContentTemplate="{DataTemplate local:LoginPage}" />
</TabBar>-->
</Shell>
<?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"
x:Class="ShellDemo.Views.LoginPage"
Shell.TabBarIsVisible="False"
Shell.NavBarIsVisible="True"
Shell.PresentationMode="ModalAnimated">
<ContentPage.Content>
<StackLayout Padding="10,0,10,0" VerticalOptions="Center">
<Label Text="Enter Your Username"/>
<Entry Placeholder="@Username..." Text="{Binding UserName}"/>
<Button VerticalOptions="Center" Text="Login" Command="{Binding LoginCommand}"/>
</StackLayout>
</ContentPage.Content>
</ContentPage>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ShellDemo.ViewModels;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
namespace ShellDemo.Views
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class LoginPage : ContentPage
{
public LoginPage()
{
InitializeComponent();
this.BindingContext = new LoginViewModel();
}
}
}
using ShellDemo.Views;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
namespace ShellDemo.ViewModels
{
public class LoginViewModel : BaseViewModel
{
private string userName;
public string UserName
{
get => userName;
set => SetProperty(ref userName, value);
}
public Command LoginCommand { get; }
public LoginViewModel()
{
LoginCommand = new Command(OnLoginClicked);
}
private async void OnLoginClicked(object obj)
{
var login = await PerformLogin();
if (!login)
return;
await Shell.Current.GoToAsync("..");
}
private async Task<bool> PerformLogin()
{
if (string.IsNullOrEmpty(userName))
{
await Shell.Current.DisplayAlert("Username Required", "Please enter your username to login", "Ok");
return false;
}
App.CurrentUserName = userName;
return true;
}
}
}
<?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:xct="http://xamarin.com/schemas/2020/toolkit"
x:Class="ShellDemo.Views.ProfilePage">
<ContentPage.Content>
<Grid HorizontalOptions="Fill"
VerticalOptions="Fill"
xct:StateLayout.CurrentState="Custom"
xct:StateLayout.CurrentCustomStateKey="{Binding CurrentView}">
<xct:StateLayout.StateViews>
<xct:StateView StateKey="Custom" CustomStateKey="LoggedOut">
<StackLayout VerticalOptions="Center" HorizontalOptions="Center">
<Label Text="Please log in to view your profile"/>
<Button Text="Login" Command="{Binding ShowLoginCommand}"/>
</StackLayout>
</xct:StateView>
<xct:StateView StateKey="Custom" CustomStateKey="LoggedIn">
<StackLayout Padding="20" Spacing="20">
<Frame HeightRequest="100" WidthRequest="100" BackgroundColor="Orange" HasShadow="False"/>
<Label Text="{Binding UserName}" FontSize="Title" FontAttributes="Bold"/>
<Label Text="Put some more detail about your profile here"/>
</StackLayout>
</xct:StateView>
</xct:StateLayout.StateViews>
</Grid>
</ContentPage.Content>
</ContentPage>
using System;
using System.Collections.Generic;
using ShellDemo.ViewModels;
using Xamarin.Forms;
namespace ShellDemo.Views
{
public partial class ProfilePage : ContentPage
{
private ProfileViewModel viewModel;
public ProfilePage()
{
InitializeComponent();
viewModel = new ProfileViewModel();
this.BindingContext = viewModel;
}
protected override void OnAppearing()
{
if (viewModel.OnAppearingCommand != null &&
viewModel.OnAppearingCommand.CanExecute(null))
{
viewModel.OnAppearingCommand.Execute(null);
}
}
}
}
using System;
using System.Diagnostics;
using ShellDemo.Views;
using Xamarin.Forms;
namespace ShellDemo.ViewModels
{
public class ProfileViewModel : BaseViewModel
{
private string currentView;
public string CurrentView
{
get => currentView;
set => SetProperty(ref currentView, value);
}
private string userName;
public string UserName
{
get => userName;
set => SetProperty(ref userName, value);
}
public Command ShowLoginCommand { get; }
public Command OnAppearingCommand { get; }
public ProfileViewModel()
{
ShowLoginCommand = new Command(OnShowLogin);
OnAppearingCommand = new Command(OnAppeared);
currentView = "LoggedOut";
}
async void OnShowLogin()
{
Debug.WriteLine("Login");
await Shell.Current.GoToAsync($"{nameof(LoginPage)}");
}
bool IsLoggedIn()
{
var userName = App.CurrentUserName;
return !string.IsNullOrEmpty(userName);
}
public void OnAppeared()
{
var loggedIn = IsLoggedIn();
if (loggedIn)
ShowLoggedIn();
else
ShowLoggedOut();
}
void ShowLoggedIn()
{
UserName = App.CurrentUserName;
CurrentView = "LoggedIn";
}
void ShowLoggedOut()
{
CurrentView = "LoggedOut";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment