之前似乎有这个工作,只是为我的应用程序创建一个登录系统。让它检查用户的本地数据库并使用用户信息创建对象。但是当用户登录登录窗口时,应该关闭并显示主窗口。
当发生这种情况时,我需要从登录窗口访问数据,如下所示:
protected override void OnStartup(System.Windows.StartupEventArgs e)
{
if (!AreSettingsSet())
{
Window LoginWindow = new Views.LoginWindow();
LoginWindow.ShowDialog();
//Waits until closed.
//If the login form was closed properly, handle the user
if (LoginWindow.DialogResult == true)
{
MessageBox.Show("Logged in correctly!");
//Add the user to the list of logged users
User returned = LoginWindow.returnUser;
MessageBox.Show("First name:" + returned.FirstName);
LoggedUsers.Add(returned);
}
else
{
//Unexpected window close, possible Alt + F4, shutdown!
MessageBox.Show(Messages.UnexpectedClose);
this.Shutdown();
}
// Recheck the settings now that the login screen has been closed.
if (!AreSettingsSet())
{
}
}
this.MainWindow = new Views.Main();
this.MainWindow.Show();
}
这是LoginWindow:
//Give App access to user object outside of this form
public User returnUser
{
get
{
return user;
}
}
//Public user object, start empty
User user = new User();
//Check the login
private void doLogin(string email, string password)
{
//Connect to database
using (SqlConnection myConnection = new SqlConnection(Settings.ConnectionString))
{
//Try and open the connection
try
{
myConnection.Open();
}
catch (Exception)
{
//Unable to connect to database, just return
MessageBox.Show(Messages.UnableOpenConnection);
return;
}
string salt = null;
bool found = false;
using (SqlCommand command = new SqlCommand(Queries.GetSalt, myConnection))
{
//Fetch the salt for the entered email address
command.Parameters.Add(new SqlParameter("@email", email));
SqlDataReader reader = command.ExecuteReader();
//Read the data
reader.Read();
if (reader.HasRows)
{
salt = reader["salt"].ToString();
found = true;
}
//Close the reader
reader.Close();
}
if (found == true)
{
if (salt.Length == 32)
{
using (SqlCommand command = new SqlCommand(Queries.GetSingleUser, myConnection))
{
//Salt the password
string saltedPassword = Encryption.sha256(password + salt);
//Add paramaters
command.Parameters.Add(new SqlParameter("@email", email));
command.Parameters.Add(new SqlParameter("@password", saltedPassword));
SqlDataReader reader = command.ExecuteReader();
reader.Read();
if (reader.HasRows)
{
//Populate the login instance
user.ID = Convert.ToDecimal(reader["id"]);
user.FirstName = reader["firstname"].ToString();
user.LastName = reader["lastname"].ToString();
user.Email = reader["email"].ToString();
user.Permissions = Convert.ToInt16(reader["permissions"]);
//Close the reader
reader.Close();
//See if user has a thumbnail picture and save it's location
string thumbLoc = Directory.GetCurrentDirectory() +
"\\Users\\DisplayPictures\\" +
user.FirstName + user.LastName + ".png";
if (File.Exists(@thumbLoc))
{
user.ThumbLoc = thumbLoc;
}
else
{
user.ThumbLoc = Directory.GetCurrentDirectory() + "\\Users\\DisplayPictures\\user.png";
}
//Found user and created object, close this window safley
this.DialogResult = true;
this.Close();
}
else
{
//Unable to find a user
MessageBox.Show(Messages.NoUserFound);
return;
}
}
}
else
{
//Salt is the incorrect length
MessageBox.Show(Messages.InvalidSalt);
return;
}
}
else
{
MessageBox.Show(Messages.NoUserFound);
}
}
}
我在这里收到错误User returned = LoginWindow.returnUser;
说:
' System.Windows.Window'不包含< returnUser'的定义 并且没有任何扩展方法&return;用户'接受第一个论点 键入System.Windows.Window'可以找到(你错过了使用 指令或程序集引用?)
请帮我解决此问题,如果我发现登录错误,请提出更改建议!
答案 0 :(得分:1)
那是因为您声明了LoginWindow
类型的Window
变量,而不是LoginWindow
。 LoginWindow
具有returnUser
属性,但Window
没有。Views.LoginWindow loginWindow = new Views.LoginWindow();
。将声明更改为:
var loginWindow = new Views.LoginWindow();
或只是
camelCase
顺便说一句,C#中的约定是在PascalCase
中命名局部变量,而不是PascalCase
。对于类型和公共成员(方法,属性等),您应该使用LoginWindow
。因此,loginWindow
变量应命名为returnUser
,ReturnUser
属性应为{{1}}。