簡體   English   中英

移至下一頁后清除條目文本

[英]Clear Entry text after moving to next page

我想在成功登錄后清除密碼條目的文本並移動到下一頁。

登錄ViewModel.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));

        }

登錄頁面.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>

登錄頁面.xaml.cs

public partial class SignInPage
{
    public SignInPage()
    {
        InitializeComponent();
        ViewModel = new SignInViewModel(new PageService());

    }

    private SignInViewModel ViewModel
    {
        get => BindingContext as SignInViewModel;
        set => BindingContext = value;
    }
}

我希望當用戶登錄成功並且應用程序進入下一頁時,PasswordEntry 將被初始化為空字符串

您的 ViewModel 未實現INotifyPropertyChanged ,無法通知 UI 進行更改。

您可以使用以下類作為 SignInViewModel 的基類。

    public class BaseViewModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }

在 SignInViewModel 中,像這樣更改您的屬性:

private string emailEntry;


    public string EmailEntry
    {
        get => emailEntry;
        set
        {
            emailEntry = value;
            OnPropertyChanged();
        }
    }

private string passwordEntry;

    public string PasswordEntry
    {
        get => passwordEntry;
        set
        {
            passwordEntry = value;
            OnPropertyChanged();
        }
    }

當您在代碼隱藏中設置視圖模型時,您可以保持簡單:

BindingContext = new SignInViewModel(new PageService());

現在,當 EmailEntry 設置為空字符串時,密碼應該為空。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM