触发方法取决于第二个窗口上的操作
请有人告诉我在以下场景中将使用的技术。
我想在允许我的代码执行另一个操作之前对用户进行身份验证。
我有一种方法可以打开一个新窗口,其中包含我的身份验证表单(用户名和密码)。
private bool userLogin()
{
Window loginInterface = new Window()
{
Title = "Please Login",
Content = new login(),
Height = 282,
Width = 300,
ResizeMode = ResizeMode.NoResize,
WindowStartupLocation = WindowStartupLocation.CenterOwner
};
loginInterface.Owner = this;
loginInterface.ShowDialog();
return true;
}
我像这样调用此方法,单击按钮时:
private void perform_action(object sender, RoutedEventArgs e)
{
if (!userLogin())
{
// Failed login, do nothing
}
else
{
// Authentication successful, perform action
delete_item();
}
}
窗口打开正常,但现在如何根据用户在打开的表单上执行的操作使我的方法返回 true 或 false?
因此,当用户单击名为 login_button
的登录按钮时,我的代码已经验证了凭据,但我需要发回“bool”值。
我可以让我的第一个窗口几乎等待在另一个窗口上执行操作并获取响应吗?
Please can someone tell me the technique that would be used, for the following scenario.
I would like to authenticate users, before I allow my code to perform another action.
I have a method that opens a new window that contains my authentication form (username and password).
private bool userLogin()
{
Window loginInterface = new Window()
{
Title = "Please Login",
Content = new login(),
Height = 282,
Width = 300,
ResizeMode = ResizeMode.NoResize,
WindowStartupLocation = WindowStartupLocation.CenterOwner
};
loginInterface.Owner = this;
loginInterface.ShowDialog();
return true;
}
I'm calling this method like so, on button click:
private void perform_action(object sender, RoutedEventArgs e)
{
if (!userLogin())
{
// Failed login, do nothing
}
else
{
// Authentication successful, perform action
delete_item();
}
}
The window opens fine, but how can I now make my method return true or false based on the what the user does on the opened form?
So when the user clicks the login button named login_button
, my code already validates the credentials, but I need the 'bool' value sent back.
Can I make my first window almost wait for an action to be performed on another window and get the response back?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Window.ShowDialog()
方法实际上已经返回一个bool?
。可以通过设置(例如)this.DialogResult = true
在Window
内的任何点进行设置。然后,您可以关闭窗口并从调用代码访问该值。要关闭带有结果的窗口:
...然后在调用代码中使用该结果:
The
Window.ShowDialog()
method actually already returns abool?
. This can be set at any point from within theWindow
by setting (for example)this.DialogResult = true
. You can then close the window and access the value from the calling code.To close the window with a result:
...and then to use that result in the calling code:
要关闭登录屏幕,您可以设置
DialogResult< /code>
为 true 或 false,以及
ShowDialog
返回此值。对于其他事情,您可以在第二个窗口上创建事件并订阅它们在第一个。To close the login screen you can set
DialogResult
to true or false, andShowDialog
returns this value. For other things you can create events on the second window and subscribe to them on the first.userLogin
应该返回 true 以外的内容。我会做这样的事情(基于所示的代码):
userLogin
should return something other than true.I would do something like this (based on the code shown):