我如何将用户从视图模型导航到另一个页面
how i can navigate user to another page from 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;
}
}
}
}
这是我的 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)
{
}
}
}
这是我的 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>
如果用户名或密码错误,请帮助我将用户导航到仪表板页面或错误消息
您可以在设置AccessToken
后使用PushAsync
或PushModalAsync
例如:
await App.Current.MainPage.Navigation.PushAsync(new dashboardPage(), true);
为了显示来自视图模型的警报,您可以使用:
await Application.Current.MainPage.DisplayAlert("Error", "Login message here", "Dismiss");
我如何在获得访问令牌和用户成功登录后将用户导航到仪表板页面,或者当用户输入错误的用户名或密码时出现错误消息
这是我的登录视图模型
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;
}
}
}
}
这是我的 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)
{
}
}
}
这是我的 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>
如果用户名或密码错误,请帮助我将用户导航到仪表板页面或错误消息
您可以在设置AccessToken
PushAsync
或PushModalAsync
例如:
await App.Current.MainPage.Navigation.PushAsync(new dashboardPage(), true);
为了显示来自视图模型的警报,您可以使用:
await Application.Current.MainPage.DisplayAlert("Error", "Login message here", "Dismiss");