I want to clear the text of the password Entry after successfull login and move to the next page.
SignInViewModel.cs
public string EmailEntry { get; set; }
public string PasswordEntry { get; set; }
private async Task LogIn()
{
var user = _userService.LoginUser(EmailEntry);
if (user == null)
{
await _pageService.DisplayAlert("Alert", "Invalid Credentials", "Ok");
return;
}
if (user.Password != PasswordEntry)
{
await _pageService.DisplayAlert("Alert", "Invalid Credentials", "Ok");
return;
}
PasswordEntry = "";
await _pageService.PushAsync(new HomePage(user));
}
SignInPage.xaml
<StackLayout VerticalOptions="Center" x:DataType="viewModels:SignInViewModel">
<Entry Placeholder="Email" Text="{Binding EmailEntry}"/>
<Entry Placeholder="Password" Text="{Binding PasswordEntry}" IsPassword="True"/>
<Button
Command="{Binding LoginCommand}"
Text="Login"
Margin="0, 10, 0, 0"
CornerRadius="10"/>
</StackLayout>
SignInPage.xaml.cs
public partial class SignInPage
{
public SignInPage()
{
InitializeComponent();
ViewModel = new SignInViewModel(new PageService());
}
private SignInViewModel ViewModel
{
get => BindingContext as SignInViewModel;
set => BindingContext = value;
}
}
I want that when user login successfully and app goes to the next page then PasswordEntry will be initialized to Empty string
Your ViewModel doesn't implement INotifyPropertyChanged , there is no way to inform the UI for a change.
You can use the following class as a base class for your SignInViewModel.
public class BaseViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
In SignInViewModel, Change your properties like this :
private string emailEntry;
public string EmailEntry
{
get => emailEntry;
set
{
emailEntry = value;
OnPropertyChanged();
}
}
private string passwordEntry;
public string PasswordEntry
{
get => passwordEntry;
set
{
passwordEntry = value;
OnPropertyChanged();
}
}
When you set your view model in the code-behind, you can keep things simple :
BindingContext = new SignInViewModel(new PageService());
Now the password should be empty when EmailEntry is set an empty string.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.