Tôi đang tạo Đăng nhập bằng cách sử dụng window controlđể cho phép người dùng đăng nhập vào WPFứng dụng mà tôi đang tạo.
Cho đến nay, tôi đã tạo ra một phương pháp kiểm tra xem liệu người dùng đã nhập đúng thông tin đăng nhập cho usernamevà passwordtrong một textboxtrên màn hình đăng nhập, bindinghai hay không properties.
Tôi đã đạt được điều này bằng cách tạo ra một boolphương pháp, như vậy;
public bool CheckLogin()
{
var user = context.Users.Where(i => i.Username == this.Username).SingleOrDefault();
if (user == null)
{
MessageBox.Show("Unable to Login, incorrect credentials.");
return false;
}
else if (this.Username == user.Username || this.Password.ToString() == user.Password)
{
MessageBox.Show("Welcome " + user.Username + ", you have successfully logged in.");
return true;
}
else
{
MessageBox.Show("Unable to Login, incorrect credentials.");
return false;
}
}
public ICommand ShowLoginCommand
{
get
{
if (this.showLoginCommand == null)
{
this.showLoginCommand = new RelayCommand(this.LoginExecute, null);
}
return this.showLoginCommand;
}
}
private void LoginExecute()
{
this.CheckLogin();
}
Tôi cũng có một nút commandmà tôi bindđể nút của tôi trong xamltương tự như vậy;
<Button Name="btnLogin" IsDefault="True" Content="Login" Command="{Binding ShowLoginCommand}" />
Khi tôi nhập tên người dùng và mật khẩu, nó sẽ thực thi mã đã chiếm đoạt, cho dù nó đúng hay sai. Nhưng làm cách nào để đóng cửa sổ này từ ViewModel khi cả tên người dùng và mật khẩu đều đúng?
Trước đây tôi đã thử sử dụng một dialog modalnhưng nó không hoàn toàn hiệu quả. Hơn nữa, trong app.xaml của tôi, tôi đã làm điều gì đó giống như sau, tải trang đăng nhập trước, sau đó khi đúng, tải ứng dụng thực tế.
private void ApplicationStart(object sender, StartupEventArgs e)
{
Current.ShutdownMode = ShutdownMode.OnExplicitShutdown;
var dialog = new UserView();
if (dialog.ShowDialog() == true)
{
var mainWindow = new MainWindow();
Current.ShutdownMode = ShutdownMode.OnMainWindowClose;
Current.MainWindow = mainWindow;
mainWindow.Show();
}
else
{
MessageBox.Show("Unable to load application.", "Error", MessageBoxButton.OK);
Current.Shutdown(-1);
}
}
Câu hỏi: Làm cách nào để đóng Đăng nhập Window controltừ ViewModel?
Cảm ơn trước.