如何在 VS 2010 中正确显示 MSTest 方法的对话框?

发布于 2024-09-10 18:53:34 字数 1114 浏览 4 评论 0原文

我最近将 MSTest 项目升级到 .NET 4.0 和 VS 2010。其中一些测试会查询外部供应商服务,从而提示用户提供必要的凭据,以便通过我们的公司 Web 代理进行通信。这过去在 vs2008 中工作得很好,但升级后,只有当用户在开始测试后立即将焦点从 VS 切换到另一个应用程序时,才会显示该对话框。显示对话框时有什么特别需要做的吗?我能想到的最好的情况是,随着 VS 的重新设计,引入了一些 WPF 警告。

有问题的代码

private void PromptUser()
        {
            if (!credentialsSet)
            {
                using (CredentialsDialog dialog = new CredentialsDialog(true))
                {
                    Process process = Process.GetCurrentProcess();
                    IWin32Window window = Control.FromHandle(process.MainWindowHandle);
                    DialogResult dr = dialog.ShowDialog(window);

                    if (dr == DialogResult.Cancel)
                    {
                        throw new InvalidOperationException("Credentials not entered");
                    }

                    credentials = dialog.Credentials;
                    user = dialog.Username;
                    password = dialog.Password;
                    domain = dialog.Domain;
                }

                credentialsSet = true;
            }
        }

I recently upgraded an MSTest project to .NET 4.0 and VS 2010. Several of the tests query an outside vendor service and thus prompt the user for necessary credentials to communicate through our corporate web proxy. This used to work fine in vs2008 but after the upgrade the dialog will only display if the user switches focus from VS to another app immediately after kicking off the tests. Is there anything special that needs to be done when displaying the dialog? The best I can figure is that there is some WPF caveat that got introduced with the redesign of VS.

The code in question

private void PromptUser()
        {
            if (!credentialsSet)
            {
                using (CredentialsDialog dialog = new CredentialsDialog(true))
                {
                    Process process = Process.GetCurrentProcess();
                    IWin32Window window = Control.FromHandle(process.MainWindowHandle);
                    DialogResult dr = dialog.ShowDialog(window);

                    if (dr == DialogResult.Cancel)
                    {
                        throw new InvalidOperationException("Credentials not entered");
                    }

                    credentials = dialog.Credentials;
                    user = dialog.Username;
                    password = dialog.Password;
                    domain = dialog.Domain;
                }

                credentialsSet = true;
            }
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

无声情话 2024-09-17 18:53:34

我认为要求提供凭据是不好的做法。您是否考虑过将导出的凭据添加到测试程序集的配置中。

如果您尝试将其设置为自动构建,您将遇到很多问题。

I would sugest that requiring the credentials is bad practice. Have you conidered adding your credentials in an excrypted fasion to the config of the test assembly.

You will run into a lot of problems if you try to setup this for automated build.

静谧幽蓝 2024-09-17 18:53:34

您可能需要激活该对话框才能使其在您激活的任何程序上弹出。

using (CredentialsDialog dialog = new CredentialsDialog(true))
{
    Process process = Process.GetCurrentProcess();
    IWin32Window window = Control.FromHandle(process.MainWindowHandle);
    dialog.Activate();
    DialogResult dr = dialog.ShowDialog(window);

    // ...
}

http://msdn.microsoft.com/ en-us/library/system.windows.forms.form.activate.aspx

You may need to activate the dialog in order to get it to pop up over whatever programs you have active.

using (CredentialsDialog dialog = new CredentialsDialog(true))
{
    Process process = Process.GetCurrentProcess();
    IWin32Window window = Control.FromHandle(process.MainWindowHandle);
    dialog.Activate();
    DialogResult dr = dialog.ShowDialog(window);

    // ...
}

http://msdn.microsoft.com/en-us/library/system.windows.forms.form.activate.aspx

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