我在silverlight工作,我创建了一个子窗口,其中我有用户名和密码。
private void OKButton_Click(object sender, RoutedEventArgs e)
{
if (txtUsrname.Text == "Username" && txtPassword.Password == "Password")
{
this.DialogResult = true;
}
else
{
MessageBox.Show("Incorrect username and/orpassword","Error",MessageBoxButton.OK);
}
}
private void CancelButton_Click(object sender, RoutedEventArgs e)
{
this.DialogResult = false;
}
然后我的主页包含整个项目GUI。就像这样:( Herei也尝试opop一个ChildWindow,但它不会这样做但是当我放一个MessageBox.Show(" Hello& #34;);然后它被填充)
public partial class MainPage : UserControl
{
public MainPage()
{
loginChildWindow obj = new loginChildWindow();
obj.Show(); //It do not pop up actually
MessageBox.Show("hello"); //whereas thsi messagebox popups
InitializeComponent();
}
}
因此,要在我的项目启动时设置登录ChildWindow,我认为只有在我的密码和userName匹配时才应该InitializeComponent();
。所以这个InitializeComponent();必须在
if (txtUsrname.Text == "Username" && txtPassword.Password == "Password")
{
InitializeComponent();
}
但是当我这样做时它会给出错误(显然我不能在构造函数中这样做):
public partial MainPage : UserControl
{
{
public MainPage()
{
private void OKButton_Click(object sender, RoutedEventArgs e)
{
if (txtUsrname.Text == "Username" && txtPassword.Password == "Password")
{
this.DialogResult = true;
InitializeComponent();
}
else
{
MessageBox.Show("Incorrect username and/orpassword","Error",MessageBoxButton.OK);
}
}
private void CancelButton_Click(object sender, RoutedEventArgs e)
{
this.DialogResult = false;
}
}
但如何解决这个问题是我的问题?
在User1评论后编辑: 这两个类是:
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
Loaded += MainPage_Loaded;
}
private void MainPage_Loaded(object sender, RoutedEventArgs e)
{
Visibility = Visibility.Collapsed;
loginChildWindow log = new loginChildWindow();
log.Show();
}
}
和
public partial class loginChildWindow : ChildWindow
{
public loginChildWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
if (txtUsrname.Text == "Username" && txtPassword.Password == "Password")
{
MainPage obj = new MainPage();
obj.Visibility = Visibility.Visible;
obj.InitializeComponent();
this.DialogResult = true;
}
else
{
MessageBox.Show("Incorrect username and/orpassword", "Error", MessageBoxButton.OK);
}
}
private void CancelButton_Click(object sender, RoutedEventArgs e)
{
this.DialogResult = false;
}
private void Login_Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
}
}
答案 0 :(得分:2)
尝试:
public MainPage()
{
InitializeComponent();
LoginWindow loginWnd=new LoginWindow();
loginWnd.Closed+= new EventHandler(loginWnd_Closed);
loginWnd.Show();
}
void loginWnd_Closed(object sender, EventArgs e)
{
LoginWindow lw = (LoginWindow)sender;
if (lw.DialogResult == true && lw.nameBox.Text != string.Empty)
{
this.textBlock1.Text = "Hello " + lw.nameBox.Text;
}
else if (lw.DialogResult == false)
{
this.textBlock1.Text = "Login canceled.";
}
}
其中LoginWindow是:
public LoginWindow()
{
InitializeComponent();
}
private void OKButton_Click(object sender, RoutedEventArgs e)
{
this.DialogResult = true;
}
private void CancelButton_Click(object sender, RoutedEventArgs e)
{
this.DialogResult = false;
}
private void LoginWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
if (this.DialogResult == true && (this.nameBox.Text == string.Empty || this.passwordBox.Password == string.Empty))
{
e.Cancel = true;
ChildWindow cw = new ChildWindow();
cw.Content = "Please Enter your name and password or click Cancel.";
cw.Show();
}
}
请参阅以下链接了解更多信息