2016-11-30 3 views
2

私のプログラムはフィットネストラッカーです。ユーザーをログインさせることはできますが、一度そのユーザー名(一意の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(); 
    } 
} 
+0

が、これは上記行きます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

+2

投稿の下に[編集](http://stackoverflow.com/posts/40896032/edit)ボタンがあります。これを使用して、ポストにコードや詳細情報を追加することができます。 –

+1

あなたはPropertyの知識だけでなく、あなたがやろうとしている情報を保持できるutilsクラスを作成する方法を知っていますか?これはVariablesやAuto Propertiesを読む必要があります。 ShowDialalogと他のフォームを閉じるときにいつでも割り当てることができるように変数を渡す方法 – MethodMan

答えて

1

これを行うには多くの方法があります。あなたが探しているのは良い古い静的クラスです。

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

、あなたのコード内で MyShaderData.UserID = "123" または何を使用することができ、いくつかのクラスを作成します。このクラスはインスタンス化する必要もなく(この場合はインスタンス化する必要があります)

+1

フィールドを使用しないでください。プロパティ。最終的にグローバルなフィールドがあなたを噛んでしまいます – JoshBerke

2

この猫には多くの方法があります。いくつかの他のより良い。

単純な静的ユーティリティクラス

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

あなたはデータを設定または取得するには、フォームのいずれかからこのクラスを使用することができます。

DIコンテナ

別のアプローチは、DIコンテナを使用することです。私は、DIコンテナをアップ配線方法についてはよく分からないが、あなたはあなたがスレッド上で現在のプリンシパルを設定することができIPrincipal あなたは

必要依存関係に渡すことができます。

System.Threading.Thread.CurrentPrincipal=new System.Security.Principal.GenericPrincipal(new System.Security.Principal.GenericIdentity("josh"),null) 
関連する問題