关闭主窗口时如何刷新登录窗口

时间:2019-04-24 09:05:25

标签: c# wpf visual-studio

我正在使用WPF创建一个小型管理应用程序。它有两个窗口。登录窗口和主窗口。当然,登录窗口只是用户名和密码。主窗口显示来自数据库的数据。问题是;当我登录时,将打开主窗口,并且由于登录窗口中的动画,用户名和密码都消失了。关闭主窗口时,如何刷新登录窗口?谢谢!

登录屏幕的代码;

public partial class LoginScreen : Window
{
    public LoginScreen()
    {
        InitializeComponent();
    }

    // Verify hashed password
    public static bool VerifyHashedPassword(string hashedPassword, string password)
    {

        byte[] buffer4;
        if (hashedPassword == null)
        {
            return false;
        }
        if (password == null)
        {
            throw new ArgumentNullException("password");
        }
        byte[] src = Convert.FromBase64String(hashedPassword);
        if ((src.Length != 0x31) || (src[0] != 0))
        {
            return false;
        }
        byte[] dst = new byte[0x10];
        Buffer.BlockCopy(src, 1, dst, 0, 0x10);
        byte[] buffer3 = new byte[0x20];
        Buffer.BlockCopy(src, 0x11, buffer3, 0, 0x20);
        using (Rfc2898DeriveBytes bytes = new Rfc2898DeriveBytes(password, dst, 0x3e8))
        {
            buffer4 = bytes.GetBytes(0x20);
        }
        return ByteArraysEqual(buffer3, buffer4);
    }

    private static bool ByteArraysEqual(byte[] buffer3, byte[] buffer4)
    {
        bool areEqual = buffer3.SequenceEqual(buffer4);

        return areEqual;
    }

    private void Button_Click_1(object sender, EventArgs e)
    {



        MySqlConnection conn = new MySqlConnection
        {
            ConnectionString = "server=db.***.**;database=***;uid=***;pwd=***;"
        };


        MySqlCommand cmd = conn.CreateCommand();
        cmd.CommandText = "SELECT brukernavn, passord FROM admin_user WHERE brukernavn = '" + txtUsername.Text + "' AND passord ='" + txtPassword.Password + "'";

        conn.Open();

        cmd.ExecuteNonQuery();


        MySqlDataReader reader = cmd.ExecuteReader();

        String dbUsername = string.Empty;
        String dbPassword = string.Empty;


        while (reader.Read())
        {
            dbUsername += reader.GetString("brukernavn");
            dbPassword += reader.GetString("passord");

        }

        if (dbUsername == "" || dbPassword == "") 
        {
            Debug.WriteLine("Wrong username and Password");
            string message = "Wrong username or password";
            string title = "Error";
            MessageBox.Show(message, title);

            this.Show();
            LoginScreen mm = new LoginScreen();
            mm.Show();
        }
        else 
        {
            Debug.WriteLine("Success");

            this.Show();
            MainWindow mm = new MainWindow();
            mm.Show();
        }
    }
}

}

这是主窗口的代码;

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void Button_Click(object sender, RoutedEventArgs e) { }


    private void Show_Users_Click(object sender, RoutedEventArgs e)
    {
        MySqlConnection conn = new MySqlConnection
        {
            ConnectionString = "server=db.****.**;database=***;uid=***;pwd=***;"
        }; try
        {
            conn.Open();





            string Query = "SELECT accountId, level, email, name FROM account";

            MySqlCommand createCommand = new MySqlCommand(Query, conn);



            createCommand.ExecuteNonQuery();


            MySqlDataAdapter dataAdp = new MySqlDataAdapter(createCommand);
            DataTable dt = new DataTable("account");
            dataAdp.Fill(dt);
            EditUsers.ItemsSource = dt.DefaultView;
            dataAdp.Update(dt);
            conn.Close();

        }

        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }


    private void Edit_Data_Click(object sender, RoutedEventArgs e)
    {
        MySqlConnection conn = new MySqlConnection
        {
            ConnectionString = "server=db.****.**;database=***;uid=***;pwd=***;"
        }; try
        {
            conn.Open();





            string Query = "SELECT accountId, level, email, name FROM account ";

            MySqlCommand createCommand = new MySqlCommand(Query, conn);



            createCommand.ExecuteNonQuery();


            MySqlDataAdapter dataAdp = new MySqlDataAdapter(createCommand);
            DataTable dt = new DataTable("account");
            dataAdp.Fill(dt);
            EditUsers.ItemsSource = dt.DefaultView;
            dataAdp.Update(dt);
            conn.Close();

        }

        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }
}

}

1 个答案:

答案 0 :(得分:0)

LoginScreen,您需要关闭LoginScreen屏幕

       else 
        {
            Debug.WriteLine("Success");
            this.Close();
            MainWindow mm = new MainWindow();
            mm.Show();
        }