c# - 如何跨窗口传递变量,以便它可以在每个窗口中使用,或者只是使其对我的整个程序可见?

标签 c#

我的程序是一个健身追踪器。我可以让用户登录,但一旦发生这种情况,需要跨窗口传递用户信息,以便他们的用户名(他们的唯一标识符)可以用来正确跟踪文件中的信息。

登录窗口代码

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();
    }
}

个人资料窗口(我想要将登录用户信息传递到的窗口之一)

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();
    }
}

最佳答案

给这只猫剥皮的方法有很多。有些比其他更好。

简单静态实用程序类

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

您可以在任何表单中使用此类来设置或获取数据。

DI 容器

另一种方法是使用 DI 容器。我不确定如何连接 DI 容器,但您可以传入所需的依赖项

IP校长 您可以在线程上设置当前主体:

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

关于c# - 如何跨窗口传递变量,以便它可以在每个窗口中使用,或者只是使其对我的整个程序可见?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40896032/

相关文章:

c# - 如何将 lambda 表达式作为参数插入 mongodb 'collection.find' 方法?

c# - .NET 中的 WMI 访问是否需要管理权限?

c# - 如何将批准测试的批准文件放在自己的文件夹中?

c# - 扩展用户管理器

c# - 如何在 C# 中将文本提取/插入到 RTF 字符串中

c# - 延迟执行的最佳方法

c# - 如何在 C# 中以编程方式获取 MSI 产品版本

c# - 如何禁用单元格上的编辑模式但复选框列?

c# - 使用asp的excel行数

c# - 在 c# metro 应用程序中创建 3D 立方体