私のプログラムはフィットネストラッカーです。ユーザーをログインさせることはできますが、一度そのユーザー名(一意のID)を使用してファイル内の情報を正しく追跡できるように、ユーザー情報をウィンドウ間で渡す必要があります。どのように変数をウィンドウに渡して、すべてのウィンドウで使用できるようにするか、それともプログラム全体に変数を表示させることができますか?
ログインウィンドウのコードが
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();
}
}
プロファイルウィンドウ(iは、ユーザーのログイン情報を渡したいウィンドウのいずれか)
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();
}
}
が、これは上記行きますlogout_clickパブリック部分クラスPatientProfileWindow:ウィンドウ { パブリックPatientProfileWindow(文字列ユーザ名、リストユーザ) { InitializeComponent(); ユーザユーザ=新しいユーザ(); ユーザcurrentUser = user.GetLoggedInUser(ユーザ名、ユーザ); userDoctorName.Content = currentUser.Doctor; //これを一度追加して医師を除外する変更を行います userName.Content = currentUser.Name; userWeight.Content = currentUser.Weight; userHeight.Content = currentUser.Height; } –
astrength
投稿の下に[編集](http://stackoverflow.com/posts/40896032/edit)ボタンがあります。これを使用して、ポストにコードや詳細情報を追加することができます。 –
あなたはPropertyの知識だけでなく、あなたがやろうとしている情報を保持できるutilsクラスを作成する方法を知っていますか?これはVariablesやAuto Propertiesを読む必要があります。 ShowDialalogと他のフォームを閉じるときにいつでも割り当てることができるように変数を渡す方法 – MethodMan