gpt4 book ai didi

c# - WPF MVVM 登录凭据

转载 作者:太空宇宙 更新时间:2023-11-03 12:20:54 25 4
gpt4 key购买 nike

我是 WPF 和 MVVM 的新手,但我在理解方面取得了进展。有一个问题让我有点困惑。我有一个带有关联 ViewModel 的登录页面,用于登录应用程序。但是,每次我使用我知道正确的凭据时,它都会返回“用户名不正确”消息框。它应该打开到 WindowMain 窗口。也许是文本框绑定(bind)的问题?任何帮助将不胜感激。谢谢!

可观察对象

public abstract class ObservableObject : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;

protected virtual void OnPropertyChanged(string propName)
{
Debug.Assert(GetType().GetProperty(propName) != null);

var pc = PropertyChanged;
if (pc != null)
{
pc(this, new PropertyChangedEventArgs(propName));
}
}

protected bool SetProperty<T>(ref T field, T value, string propName)
{
if (!EqualityComparer<T>.Default.Equals(field, value))
{
field = value;
OnPropertyChanged(propName);
return true;
}

return false;
}

protected bool SetProperty<T>(ref T field, T value, Expression<Func<T>> expr)
{
if (!EqualityComparer<T>.Default.Equals(field, value))
{
field = value;
var lambda = (LambdaExpression)expr;
MemberExpression memberExpr;

if (lambda.Body is UnaryExpression)
{
var unaryExpr = (UnaryExpression)lambda.Body;
memberExpr = (MemberExpression)unaryExpr.Operand;
}
else
{
memberExpr = (MemberExpression)lambda.Body;
}

OnPropertyChanged(memberExpr.Member.Name);
return true;
}

return false;
}
}

登录中继命令

public class LoginRelayCommand : ICommand
{
private Action<object> execute;
private Func<object, bool> canExecute;

public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}

public LoginRelayCommand(Action<object> execute, Func<object, bool> canExecute = null)
{
this.execute = execute;
this.canExecute = canExecute;
}

public bool CanExecute(object parameter)
{
return this.canExecute == null || this.canExecute(parameter);
}

public void Execute(object parameter)
{
this.execute(parameter);
}
}

View 模型

public class LoginWindowVM : ObservableObject
{
private ICommand _submitLoginButton;

private Textbox1 _textbox1Input;
private Textbox2 _textbox2Input;

public ICommand SubmitLoginButton
{
get
{
if(_submitLoginButton == null)
{
_submitLoginButton = new LoginRelayCommand(param => this.SubmitButton(), null);
}

return _submitLoginButton;
}
}

public Textbox1 Textbox1Input
{
get { return _textbox1Input; }
set { SetProperty(ref _textbox1Input, value, () => Textbox1Input); }
}

public Textbox2 Textbox2Input
{
get { return _textbox2Input; }
set { SetProperty(ref _textbox2Input, value, () => Textbox2Input); }
}

public LoginWindowVM() : base()
{
_textbox1Input = new Textbox1();
_textbox2Input = new Textbox2();
}

private void SubmitButton()
{
MySqlConnection dbConnectionString = new MySqlConnection(@"datasource=localhost;port=3306;Initial Catalog='optest1a1';username=root;password=");
try
{
if (dbConnectionString.State == System.Data.ConnectionState.Closed)
dbConnectionString.Open();
String query = "SELECT COUNT(1) FROM users_sw WHERE Username=@Username AND Password=@Password";
MySqlCommand cmd = new MySqlCommand(query, dbConnectionString);
cmd.CommandType = System.Data.CommandType.Text;
cmd.Parameters.AddWithValue("@Username", Textbox1Input);
cmd.Parameters.AddWithValue("@Password", Textbox2Input);
int count = Convert.ToInt32(cmd.ExecuteScalar());
if (count == 1)
{
WindowMain dashboard = new WindowMain();
dashboard.Show();
dbConnectionString.Close();
}
else
{
MessageBox.Show("Username or Password Is Incorrect");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
dbConnectionString.Close();
}
}
}

查看

    <Grid>
<Label x:Name="lblUsername" Content="Username" HorizontalAlignment="Left" Margin="45,57,0,0" VerticalAlignment="Top"/>
<Label x:Name="lblPassword" Content="Password" HorizontalAlignment="Left" Margin="48,104,0,0" VerticalAlignment="Top"/>
<TextBox x:Name="txtUsername" Text="{Binding Textbox1Input, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
HorizontalAlignment="Left" Height="23" Margin="128,60,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
<TextBox x:Name="txtPassword" Text="{Binding Textbox2Input, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
HorizontalAlignment="Left" Height="23" Margin="128,108,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
<Button x:Name="btnSubmit" Content="Submit" HorizontalAlignment="Left" Margin="118,175,0,0" VerticalAlignment="Top" Width="75"
Command="{Binding SubmitLoginButton}"
/>
</Grid>

View.cs

    public partial class LoginWindow : Window
{
private LoginWindowVM _viewModel;

public LoginWindow()
{
InitializeComponent();

_viewModel = new LoginWindowVM();
this.DataContext = _viewModel;
}
}

最佳答案

您正在绑定(bind)到 TextBox Text 属性,该属性期望 string 作为源并且您正在绑定(bind)到 Textbox 本身。只需将其更改为 String 属性:

private String_textbox1Input;

public String Textbox1Input
{
get { return _textbox1Input; }
set { SetProperty(ref _textbox1Input, value, () => Textbox1Input); }
}

注意:文本框默认绑定(bind)模式是TwoWay,显式设置它是没有用的

关于c# - WPF MVVM 登录凭据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47343678/

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