2016-11-30 29 views
2

我的程序是一個健身追蹤器。我可以讓用戶登錄,但是一旦發生這種情況,需要跨越窗口傳遞用戶信息,以便他們的用戶名(他們的唯一標識符)可以用於在他們的文件中正確跟蹤信息。如何跨窗口傳遞變量,以便可以在每個窗口中使用變量,或者只是使其對整個程序可見?

登錄窗口代碼

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

這上面去公共部分類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

+2

您的帖子下方有一個[edit](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) 
相關問題