Visual Studio “显示/隐藏密码MVVM Xamarin表单”复选框

avwztpqn  于 2022-11-17  发布在  其他
关注(0)|答案(1)|浏览(265)

我一直在这个网站,youtube和谷歌上寻找答案,但我只想出了EventTriggersImageSource
我有一个LoginPageViewLoginPageViewModel,有一个Entry,用户必须在其中输入密码,还有一个CheckBox,允许查看输入的密码。
我已经尝试了许多方法来绑定和编码Checkbox以显示密码,但我最终要么不成功,要么只是永久显示密码。
我有**.XAML**的代码

<Entry Text="{Binding Password}"
                               TextColor="White"
                               FontSize="18"
                               FontAttributes="Bold"
                               Placeholder="Password"
                               PlaceholderColor="White"
                               IsPassword="{Binding IsPass}"
                               x:Name="PasswordBox"
                               VerticalTextAlignment="Center"/>

<CheckBox Color="White"
                              x:Name="ChkShowPass"
                              Margin="25,0,0,0"
                              IsChecked="{Binding IsCheckPass}"/>
                    
<Label Text="Show Password"
                           TextColor="White"
                           FontAttributes="Bold"
                           FontSize="17"
                           RelativeLayout.XConstraint="60"
                           RelativeLayout.YConstraint="4"/>

现在,我为LoginPageViewModel尝试的选项如下

选项1

private bool isCheckPass;
    public bool IsCheckPass
    {
        get { return isCheckPass; }
        set 
        {
            if (isCheckPass != value)
            {
                isCheckPass = value;
                if (PropertyChanged != null)
                {
                    
                }
            }
        }
    }

然后尝试**.XAML**

<ImageButton Source="ShowPass.png"
                                 x:Name="BtnShowPass"
                                 Margin="25,0,0,0"
                                 BackgroundColor="Transparent"
                                 Command="{Binding ToggleIsPassword}">
                        <ImageButton.Triggers>
                            <DataTrigger TargetType="ImageButton"
                                         Binding="{Binding IsPassword}" Value="True">
                                <Setter Property="Source" Value="HidePass.png" />
                            </DataTrigger>
                        </ImageButton.Triggers>
                    </ImageButton>

登录页面视图模型中的.cs

private bool _IsPass = true;
    public bool IsPass
    {
        get
        {
            return _IsPass;
        }
        set
        {
            _IsPass = value;
            OnPropertyChanged();
            OnPropertyChanged("IsPass");
        }
    }

    public ICommand ToggleIsPassword => new Command(() => IsPass = !IsPass);

请有人在这里协助,任何和所有的帮助欢迎。

balp4ylt

balp4ylt1#

更新

您需要一个转换器来转换它,您可以使用InverseBoolConverter,如下所示:

public class InverseBooleanConverter: IValueConverter
    {    
        public object Convert(object value, Type targetType, object parameter,
            System.Globalization.CultureInfo culture)
        {
            if (targetType != typeof(bool))
                throw new InvalidOperationException("The target must be a boolean");

            return !(bool)value;
        }

        public object ConvertBack(object value, Type targetType, object parameter,
            System.Globalization.CultureInfo culture)
        {
            throw new NotSupportedException();
        }
   }

在XAML中定义它:

<ContentPage.Resources>
 <converters:InverseBooleanConverter x:Key="InverseBooleanConverter"/>
</ContentPage.Resources>

如果我是你,我会做下面这样的事情:
在ViewModel中添加一个属性:

public bool IsPasswordVisible
{
   get => isPassword;
   set 
      {
         isPassword = value;
         OnPropertyChanged();
      }
}

然后在你的视野中,你会这样绑定它们:

<Entry Text="{Binding IsPasswordVisible, Converter={StaticResoource InverseBooleanConverter}}"
                           TextColor="White"
                           FontSize="18"
                           FontAttributes="Bold"
                           Placeholder="Password"
                           PlaceholderColor="White"
                           IsPassword="{Binding IsPass}"
                           x:Name="PasswordBox"
                           VerticalTextAlignment="Center"/>

<CheckBox Color="White"
                          x:Name="ChkShowPass"
                          Margin="25,0,0,0"
                          IsChecked="{Binding IsPasswordVisible, Mode=TwoWay}"/>

希望这对你有帮助!

相关问题