2009-03-04 135 views
0

我有一個登錄窗口,用戶名和密碼在軟件中訪問後,用戶認證我想在下一個窗口(軟件的主窗口)顯示在TextBlock中認證的用戶的名稱......我會讓我的登錄窗口的代碼片段:綁定TextBlock Linq ToSql和WPF

public partial class Window1 : Window 
{ 
    public Window1() 
    { 
     InitializeComponent(); 
    } 

    public bool ValidateApplicationUser(string userName, string password) 
    { 
     { 
      var AuthContext = new DataClasses1DataContext(); 
      var query = from c in AuthContext.Users 
         where (c.Username == userName.ToLower() && c.Password == password.ToLower()) 
         select c; 

      if(query.Count() != 0) 
      { 
       return true; 
      } 

      return false; 
     } 
    } 

    private void mahhh(object sender, RoutedEventArgs e) 
    { 
     bool authenticated = true; 
     { 
      if (usernameTextBox.Text !="" && passwordTextBox.Text != "") 
      { 
       authenticated = ValidateApplicationUser(usernameTextBox.Text , passwordTextBox.Text); 
      } 
     } 
     if (!authenticated) 
     { 
      MessageBox.Show("Invalid login. Try again."); 
     } 
     else 
     { 
      MessageBox.Show("Congradulations! You're a valid user!"); 
      MainWindow c = new MainWindow(); 
      c.ShowDialog(); 
     } 
    } 
} 

如果我的用戶名「馬克」的主窗口,我將顯示用戶名「馬克」在一個TextBlock驗證,我不知道我做它? 我該怎麼做?

+0

你可能想澄清你題。我不確定用戶名的存儲位置或顯示位置。 – 2009-03-12 18:22:34

回答

0

簡單地說,用戶名傳遞給主窗口的構造這樣

MainWindow c = New MainWindow(usernameTextBox.Text); 

,並在主窗口的構造函數接受變量的值,做任何你想做的事情,這樣的

private String _userName; 

public MainWindow(string userName) 
{ 
    _userName = userName 
} 
1

我想你在年代碼有一些錯誤(這將使空字段記錄),它必須是這樣的:

bool authenticated = true; 
    { 
     if (usernameTextBox.Text !="" && passwordTextBox.Text != "") 
     { 
      authenticated = ValidateApplicationUser(usernameTextBox.Text , passwordTextBox.Text); 
     } 


    } 
    if (!authenticated || usernameTextBox.Text == "" || passwordTextBox.Text == "") 
    { 
     MessageBox.Show("Invalid login. Try again."); 
    } 
    else 
    { 
     MessageBox.Show("Congradulations! You're a valid user!"); 
     MainWindow c = new MainWindow(); 
     c.ShowDialog(); 

    } 
0

公共變量/屬性添加到您的主窗口類

public string Username { get; set; } 

現在你可以設置該屬性

MessageBox.Show("Congradulations! You're a valid user!"); 
MainWindow c = new MainWindow(); 
c.Username = usernameTextBox.Text; 
c.ShowDialog(); 

,並用它在你的主窗口

MainWindow_Loaded(..) {  
    MessageBox.Show("You are " + Username); 
}