WPF控件:密码框绑定MVVM

打印 上一主题 下一主题

主题 1033|帖子 1033|积分 3099

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
以下是一种使用 MVVM 模式的方法:

  • 首先,在 ViewModel 中添加一个属性来生存密码,我们可以使用 SecureString 类型。
  1. // 密码变量
  2. private SecureString _password;
  3. // 密码属性,用于获取和设置密码
  4. public SecureString Password
  5. {
  6.      get
  7.      {
  8.          return _password;
  9.      }
  10.      set
  11.      {
  12.          // 如果新值与旧值不同
  13.          if (_password != value)
  14.          {
  15.              // 更新密码
  16.              _password = value;
  17.              // 触发属性更改通知,通知UI层密码已更改
  18.              RaisePropertyChanged(nameof(Password));
  19.          }
  20.      }
  21. }
复制代码
 

  • 创建一个附加属性来处置惩罚 PasswordBox 的密码变化,并将其绑定到 ViewModel 中的命令。
  1. public ICommand PasswordChangedCommand => new DelegateCommand<object>(PasswordChanged);
  2.   private void PasswordChanged(object parameter)
  3.   {
  4.       var passwordBox = parameter as PasswordBox;
  5.       if (passwordBox != null)
  6.       {
  7.           // 设置 ViewModel 中的密码属性
  8.           Password = passwordBox.SecurePassword;
  9.       }
  10.   }
复制代码
 

  • 在 XAML 中,使用活动触发器来触发命令。
  1. xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
复制代码
  1. <PasswordBox
  2.     x:Name="PasswordBox"
  3.     Height="45"
  4.     Margin="5"
  5.     FontSize="20"
  6.     FontWeight="Thin">
  7.     <i:Interaction.Triggers>
  8.         <i:EventTrigger EventName="PasswordChanged">
  9.             <i:InvokeCommandAction Command="{Binding PasswordChangedCommand}" CommandParameter="{Binding ElementName=PasswordBox}" />
  10.         </i:EventTrigger>
  11.     </i:Interaction.Triggers>
  12. </PasswordBox>
复制代码

  • 查看密码框的内容。
  1. MessageBox.Show(SecureStringToString(Password));
复制代码
  1. /// <summary>
  2. /// 将 SecureString 类型的数据转换为普通的字符串类型。
  3. /// </summary>
  4. /// <param name="secureString">要转换的 SecureString 对象。</param>
  5. /// <returns>转换后的字符串,如果转换失败则返回空字符串。</returns>
  6. private string SecureStringToString(SecureString secureString)
  7. {
  8.     // 初始化指针
  9.     IntPtr ptr = IntPtr.Zero;
  10.     try
  11.     {
  12.         // 将 SecureString 转换为指针
  13.         ptr = Marshal.SecureStringToGlobalAllocUnicode(secureString);
  14.         if (ptr != IntPtr.Zero)
  15.         {
  16.             // 将指针中的数据复制到一个普通的字符串
  17.             return Marshal.PtrToStringUni(ptr);
  18.         }
  19.         else
  20.         {
  21.             return string.Empty;
  22.         }
  23.     }
  24.     catch (Exception ex)
  25.     {
  26.         // 处理异常
  27.         Console.WriteLine($"转换 SecureString 出错:{ex.Message}");
  28.         return string.Empty;
  29.     }
  30.     finally
  31.     {
  32.         // 清除内存中的敏感数据
  33.         if (ptr != IntPtr.Zero)
  34.         {
  35.             Marshal.ZeroFreeGlobalAllocUnicode(ptr);
  36.         }
  37.     }
  38. }
复制代码
 
 
 

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

河曲智叟

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表