简体   繁体   中英

How can I pass a variable across windows so that it can be used in every window or just make it visible to my entire program?

My program is a fitness tracker. I can get the user logged in but once that happens need to pass the users information across windows so that their username(their unique identifier) can be used to track information properly in their file.

login window code

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

    private void LogIn_Click(object sender, RoutedEventArgs e)
    {
        //loads users from file into a list we can use to search for individual user information
        DataManagement loadUserList = new DataManagement();
        List<User> Users = loadUserList.ReadUsers();

        string username = Convert.ToString(userUsername.Text);
        string password = Convert.ToString(userPassword.Text);

        //checks to see if the username and password match a saved profile and allows access to
        //profile window if the user login is valid
        User user = new User();
        bool allowLogin = user.UserLogIn(username, password, Users);
        if (allowLogin == true)
        {
            //returns the user we are using to here so that we know 
            //what user we need to personalize the profile for
            PatientProfileWindow patientWindow = new PatientProfileWindow(username, Users);
            patientWindow.Show();
            this.Close();
        }
        else
        {   
            MessageBox.Show("LogIn Failed. Please Try Again");
            userUsername.Clear();
            userPassword.Clear();
        }
    }

    private void CreateNewAccount_Click(object sender, RoutedEventArgs e)
    {
        //opens window for user to create new profile
        CreateNewProfileWindow createProfile = new CreateNewProfileWindow();
        createProfile.Show();
        this.Close();
    }
}

profile window(one of the windows i want to pass the logged in users information to)

public partial class PatientProfileWindow : Window
{
    public PatientProfileWindow(string username, List<User> Users)
    {
        InitializeComponent();
        User user = new User();
        User currentUser = user.GetLoggedInUser(username, Users);
        userDoctorName.Content = currentUser.Doctor;   //add this once making changes to exclude doctors
        userName.Content = currentUser.Name;
        userWeight.Content = currentUser.Weight;
        userHeight.Content = currentUser.Height;
    }

    private void logOut_Click(object sender, RoutedEventArgs e)
    {
        UserLoginWindow login = new UserLoginWindow();
        login.Show();
        this.Close();
    }

    private void trackActivity_Click(object sender, RoutedEventArgs e)
    {
        TrackActivityWindow activityWindow = new TrackActivityWindow();
        activityWindow.Show();
        this.Close();
    }

    private void trackNutritionalIntake_Click(object sender, RoutedEventArgs e)
    {
        TrackNutritionalIntakeWindow intakeWindow = new TrackNutritionalIntakeWindow();
        intakeWindow.Show();
        this.Hide();
    }

    private void button_Click(object sender, RoutedEventArgs e)
    {
        EditProfileWindow editProfile = new EditProfileWindow();
        editProfile.Show();
        this.Hide();
    }
}

There are many ways to skin this cat. Some better then others.

Simple Static Utility Class

public static class Utils
{
     public static string Username{get;set;}
}

You can use this class from any of your forms to set or get the data.

DI Container

Another approach is to use a DI container. I'm not sure about how to wire up a DI container, but you could pass in the dependencies you need

IPrincipal You can set the current principal on the thread:

System.Threading.Thread.CurrentPrincipal=new System.Security.Principal.GenericPrincipal(new System.Security.Principal.GenericIdentity("josh"),null)

There are many ways to do this. What you are looking for is a good old static class.

create some class

public static class MySharedData {
 public static string UserID ="";
}

and the you can use in your code MyShaderData.UserID = "123" or whatever. Notice this class does not need (nor should in this case) be instantiated)

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