触发方法取决于第二个窗口上的操作

发布于 2024-12-13 12:20:13 字数 1091 浏览 0 评论 0原文

请有人告诉我在以下场景中将使用的技术。

我想在允许我的代码执行另一个操作之前对用户进行身份验证。

我有一种方法可以打开一个新窗口,其中包含我的身份验证表单(用户名和密码)。

    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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

花海 2024-12-20 12:20:13

Window.ShowDialog() 方法实际上已经返回一个 bool?。可以通过设置(例如)this.DialogResult = trueWindow 内的任何点进行设置。然后,您可以关闭窗口并从调用代码访问该值。

要关闭带有结果的窗口:

this.DialogResult = true;

...然后在调用代码中使用该结果:

var myWindow = /*create window*/;
var result = myWindow.ShowDialog();
if (result == true)
{
     //...
}

The Window.ShowDialog() method actually already returns a bool?. This can be set at any point from within the Window 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:

this.DialogResult = true;

...and then to use that result in the calling code:

var myWindow = /*create window*/;
var result = myWindow.ShowDialog();
if (result == true)
{
     //...
}
苦笑流年记忆 2024-12-20 12:20:13

要关闭登录屏幕,您可以设置 DialogResult< /code>为 true 或 false,以及 ShowDialog 返回此值。对于其他事情,您可以在第二个窗口上创建事件并订阅它们在第一个。

To close the login screen you can set DialogResult to true or false, and ShowDialog returns this value. For other things you can create events on the second window and subscribe to them on the first.

浅忆流年 2024-12-20 12:20:13

userLogin 应该返回 true 以外的内容。

我会做这样的事情(基于所示的代码):

return loginInterface.WasSuccessful;  // you'd have to add this property

userLogin should return something other than true.

I would do something like this (based on the code shown):

return loginInterface.WasSuccessful;  // you'd have to add this property
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文