简体   繁体   中英

How can I pass data between windows in my C# WPF application?

I have a login form in the MainWindow of my WPF application. If the user logs in successfully, I want to open the HomeWindow. My problem is that I need to pass the adminID variable from the MainWindow to the HomeWindow. How can I do this?

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

            private void btnLogin_Click(object sender, RoutedEventArgs e)
            {
                int errors = 0;
                if (txtUsername.Text == "")
                {
                    lblUsernameStatus.Content = "This field is required.";
                    errors = errors + 1;
                }

                if (txtPassword.Text == "")
                {
                    lblPasswordStatus.Content = "This field is required.";
                    errors = errors + 1;
                }

                if (errors == 0)
                {
                    Administrator TryLogin = new Administrator();
                    if (TryLogin.VerifyUser(txtUsername.Text, txtPassword.Text))
                    {
                        HomeWindow home = new HomeWindow();
                        int adminID = TryLogin.userID;
                        home.Show();
                        this.Close();
                    }
                    else
                    {
                        lblLoginStatus.Content = TryLogin.status;
                    }

                }
            }

PS: I haven't written anything in the HomeWindow.xaml.cs file.

Define an initializer in HomeWindow to accept the data you wish to send:

private int AdminID;

public HomeWindow()
{
    InitializeComponent();
}

public HomeWindow(int adminID) : base()
{
    AdminID = adminID;
}

Then you can just:

HomeWindow home = new HomeWindow(TryLogin.userID);
home.Show();
this.Close();

Declaring static variable would be the simplest and easiest way because once login is authorized, the value doesn't change until logged off(application is exited)

I also used a way of passing value through Window constructor but static variables are much easier to utilize many fixed data of the logged-in users like customized setting data for each users. I also have a WPF app and pass 11 values and utilize easiily everywhere inside application.

Declare as static variable in MainWindow like,

 public static int adminID;

 private void btnLogin_Click(object sender, RoutedEventArgs e)
{
    adminID= TryLogin.userID;
}

And usage in HomeWindow is like,

MainWindow.adminID

Hope this helps..

There are different ways to achieve this.

Here is an example using singleton

public class User
{
    private static readonly User _instance;
    private static object syncRoot = new Object();

    public string Id { get; set; }
    public string Username { get; set; }
    public string Password { get; set; }
    public string SomeOtherProperty { get; set; }

    private User()
    { 
        // Initialize defaults
    }

    public void Reset()
    {
        // Clear existing values
    }

    public static User Instance
    {
        get 
        { 
            if (instance == null) 
            {
                lock (syncRoot) 
                {
                    if (_instance == null) 
                        _instance = new User();
                    return _instance; 
                }
            }
        }
    }   
}

On your login form:

private void btnLogin_Click(object sender, RoutedEventArgs e)
{
    int errors = 0;
    if (txtUsername.Text == "")
    {
        lblUsernameStatus.Content = "This field is required.";
        errors = errors + 1;
    }

    if (txtPassword.Text == "")
    {
        lblPasswordStatus.Content = "This field is required.";
        errors = errors + 1;
    }

    if (errors == 0)
    {
        Administrator TryLogin = new Administrator();
        if (TryLogin.VerifyUser(txtUsername.Text, txtPassword.Text))
        {
            User.Instance.Reset(); // Make sure old data is removed
            User.Instance.Username = txtUsername.Text;
            User.Instance.Password = txtPassword.Text;

            HomeWindow home = new HomeWindow();
            int adminID = TryLogin.userID;
            home.Show();
            this.Close();
        }
        else
        {
            lblLoginStatus.Content = TryLogin.status;
        }
    }
}

On your home form:

You can retrieve the User credentials using User.Instance just make sure you reset it on logoff.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM