how i can navigate user to dashboard page after i got Access token and user successfully logged in or error messege when user entred wrong username or password
here is my login viewmodel
using RoyalSales.Views;
using System.Windows.Input;
using Xamarin.Forms;
using RoyalSalesAPI.Services;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Android.App;
using Android.Content.Res;
using RoyalSales.Helpers;
namespace RoyalSales.ViewModels
{
class LoginViewModel
{
private APIServices _APIServices = new APIServices();
public string Username { get; set; }
public string Password { get; set; }
public String Message { get; set; }
public ICommand Logincommand => new Command(async () =>
{
var accesssToken = await _APIServices.LoginAsync(Username, Password);
if (!string.IsNullOrEmpty(accesssToken))
{
Message = "login succeed";
Settings.Username = Username;
Settings.Password = Password;
Settings.AccessToken = accesssToken;
// here i want navigate user to dashboard page
}
else
{
Message = "wrong username or password";
// here i want show message dialoge to tell user theres an wrong username or
password
}
});
public ICommand LogOutcommand
{
get
{
return new Command( () =>
{
Settings.AccessToken = null;
});
}
}
public LoginViewModel()
{
if (!string.IsNullOrEmpty(Settings.Username))
{
Username = Settings.Username;
Password = Settings.Password;
}
}
}
}
here is my loginpage.cs
using Javax.Security.Auth;
using RoyalSales.Views;
using Android.Provider;
using RoyalSalesAPI.Services;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
using RoyalSales.ViewModels;
using RoyalSales.Helpers;
using Settings = RoyalSales.Helpers.Settings;
namespace RoyalSales
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class Login : ContentPage
{
public APIServices aPIServices = new APIServices();
private string accessToken = Settings.AccessToken.ToString();
public Login()
{
InitializeComponent();
NavigationPage.SetHasNavigationBar(this, false);
}
private async void Button_Clicked(object sender, EventArgs e)
{
}
}
}
here is my loginpage.xaml
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage
x:Class="RoyalSales.Login"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:viewModels="clr-namespace:RoyalSales.ViewModels;assembly=RoyalSales"
Title="login page"
BackgroundColor="#F38906">
<ContentPage.BindingContext>
<viewModels:LoginViewModel />
</ContentPage.BindingContext>
<ContentPage.Content>
<StackLayout
Padding="20"
Orientation="Vertical"
Spacing="30">
<BoxView HeightRequest="20" />
<Image
HorizontalOptions="Center"
Source="almotaheda.png"
WidthRequest="175" />
<Label
FontSize="Large"
HorizontalOptions="Center"
Text="slogn"
TextColor="White" />
<Frame BackgroundColor="#FBD7AC" HasShadow="False">
<StackLayout Orientation="Vertical" Spacing="10">
<Entry
x:Name="UserNameEntry"
HeightRequest="40"
HorizontalTextAlignment="Center"
Placeholder="username"
PlaceholderColor="#F38906"
Text="{Binding Username}"
TextColor="Black" />
<Entry
x:Name="PasswordEntry"
HeightRequest="40"
HorizontalTextAlignment="Center"
IsPassword="True"
Placeholder="Password"
PlaceholderColor="#F38906"
Text="{Binding Password}"
TextColor="Black" />
</StackLayout>
</Frame>
<Label
FontSize="20"
Text="{Binding Message}"
TextColor="#FFFFFF" />
<Button
BackgroundColor="White"
Command="{Binding Logincommand}"
FontAttributes="Bold"
FontSize="20"
HorizontalOptions="FillAndExpand"
Text="login"
TextColor="#F38906"
Clicked="Button_Clicked"/>
<BoxView HeightRequest="20" />
</StackLayout>
</ContentPage.Content>
</ContentPage>
plese help my to navigate the user to dashboard page if successfuly logged in or error message if wrong username or password
You can use
PushAsync
orPushModalAsync
after setting theAccessToken
For example:
In order to display an Alert from viewmodel you can use: