gpt4 book ai didi

c# - Caliburn.Micro是否支持PasswordBox?

转载 作者:行者123 更新时间:2023-12-01 16:35:20 27 4
gpt4 key购买 nike

位于http://caliburnmicro.com的Caliburn.Micro主页提出以下声明,但我无法使用该示例中可以想到的任何变体使CM与PasswordBox控件一起使用。因为名称不同,所以看不到它如何工作。有谁有一个允许我获取PasswordBox值的CM示例?是否需要特定版本的CM?我正在运行CM的1.5.2版。理想情况下,不使用“附加属性”,但如果可以与CM一起使用,则只能这样做。请不要就安全问题进行任何讲座,因为在我看来这不是问题。



使用参数和保护方法自动在视图和视图模型之间应用方法

<StackPanel>
<TextBox x:Name="Username" />
<PasswordBox x:Name="Password" />
<Button x:Name="Login" Content="Log in" />
</StackPanel>




public bool CanLogin(string username, string password)
{
return !String.IsNullOrEmpty(username) && !String.IsNullOrEmpty(password);
}

public string Login(string username, string password)
{
...
}

最佳答案

我只能使它与依赖项属性一起使用,有效地绕过了Caliburn.Micro提供的约定绑定优点。我认识到这不是您的理想选择,但务实地这是我经常使用的解决方案。我相信,当我历史上遇到这种障碍时,我在StackOverflow上发现了this post并引导我朝这个方向发展。供您考虑:

public class BoundPasswordBox
{
private static bool _updating = false;

/// <summary>
/// BoundPassword Attached Dependency Property
/// </summary>
public static readonly DependencyProperty BoundPasswordProperty =
DependencyProperty.RegisterAttached("BoundPassword",
typeof(string),
typeof(BoundPasswordBox),
new FrameworkPropertyMetadata(string.Empty, OnBoundPasswordChanged));

/// <summary>
/// Gets the BoundPassword property.
/// </summary>
public static string GetBoundPassword(DependencyObject d)
{
return (string)d.GetValue(BoundPasswordProperty);
}

/// <summary>
/// Sets the BoundPassword property.
/// </summary>
public static void SetBoundPassword(DependencyObject d, string value)
{
d.SetValue(BoundPasswordProperty, value);
}

/// <summary>
/// Handles changes to the BoundPassword property.
/// </summary>
private static void OnBoundPasswordChanged(
DependencyObject d,
DependencyPropertyChangedEventArgs e)
{
PasswordBox password = d as PasswordBox;
if (password != null)
{
// Disconnect the handler while we're updating.
password.PasswordChanged -= PasswordChanged;
}

if (e.NewValue != null)
{
if (!_updating)
{
password.Password = e.NewValue.ToString();
}
}
else
{
password.Password = string.Empty;
}
// Now, reconnect the handler.
password.PasswordChanged += PasswordChanged;
}

/// <summary>
/// Handles the password change event.
/// </summary>
static void PasswordChanged(object sender, RoutedEventArgs e)
{
PasswordBox password = sender as PasswordBox;
_updating = true;
SetBoundPassword(password, password.Password);
_updating = false;
}
}


然后,在您的XAML中:

<PasswordBox pwbx:BoundPasswordBox.BoundPassword="{Binding UserPassword, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged,NotifyOnValidationError=True,ValidatesOnDataErrors=True}" />


并且pwbx在Window标记上被发现为命名空间:

<Window x:Class="MyProject.Views.LoginView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
xmlns:pwbx="clr-namespace:MyProject.Client.Controls">


ViewModel:

using Caliburn.Micro;
using MyProject.Core;
using MyProject.Repositories;
using MyProject.Types;
using MyProject.ViewModels.Interfaces;

namespace MyProject.ViewModels
{
public class LoginViewModel : Screen, ILoginViewModel
{
private readonly IWindowManager _windowManager;
private readonly IUnitRepository _unitRepository;
public bool IsLoginValid { get; set; }
public Unit LoggedInUnit { get; set; }

private string _password;
public string UserPassword
{
get { return _password; }
set
{
_password = value;
NotifyOfPropertyChange(() => UserPassword);
NotifyOfPropertyChange(() => CanLogin);
}
}

private string _name;
public string Username
{
get { return _name; }
set
{
_name = value;
NotifyOfPropertyChange(() => Username);
NotifyOfPropertyChange(() => CanLogin);
}
}
public LoginViewModel(IWindowManager windowManager,IUnitRepository unitRepository)
{
_windowManager = windowManager;
_unitRepository = unitRepository;
DisplayName = "MyProject - Login";
Version = ApplicationVersionRepository.GetVersion();
}

public string Version { get; private set; }

public void Login()
{
// Login logic
var credentials = new UserCredentials { Username = Username, Password=UserPassword };

var resp = _unitRepository.AuthenticateUnit(credentials);
if (resp == null) return;
if (resp.IsValid)
{
IsLoginValid = true;
LoggedInUnit = resp.Unit;
TryClose();
}
else
{
var dialog = new MessageBoxViewModel(DialogType.Warning, DialogButton.Ok, "Login Failed", "Login Error: " + resp.InvalidReason);
_windowManager.ShowDialog(dialog);
}
}

public bool CanLogin
{
get
{
return !string.IsNullOrEmpty(Username) && !string.IsNullOrEmpty(UserPassword);
}
}
}
}

关于c# - Caliburn.Micro是否支持PasswordBox?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30631522/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com