我的程序是一個健身追蹤器。我可以讓用戶登錄,但是一旦發生這種情況,需要跨越窗口傳遞用戶信息,以便他們的用戶名(他們的唯一標識符)可以用於在他們的文件中正確跟蹤信息。如何跨窗口傳遞變量,以便可以在每個窗口中使用變量,或者只是使其對整個程序可見?
登錄窗口代碼
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();
}
}
這上面去公共部分類PatientProfileWindow:Window { Public PatientProfileWindow(string username,List Users) { InitializeComponent(); User user = new User(); User currentUser = user.GetLoggedInUser(username,Users); userDoctorName.Content = currentUser.Doctor; //添加一次,進行更改以排除醫生 userName.Content = currentUser.Name; userWeight.Content = currentUser.Weight; userHeight.Content = currentUser.Height; } –
astrength
您的帖子下方有一個[edit](http://stackoverflow.com/posts/40896032/edit)按鈕。您可以使用它來向您的帖子添加代碼和更多信息。 –
是否熟悉Property以及如何創建一個可以容納您想要的信息的utils類..這並不難,您需要閱讀Variables和Auto Properties以及有關詳細信息。 ShowDialalog以及如何傳回變量,以便在關閉其他表單時可以隨時分配變量以及可用的變量。 – MethodMan