有什么方法可以在“启动”之前显示 Windows 窗体吗?表单已加载到 VB.NET 中?

发布于 2024-10-08 17:02:34 字数 1097 浏览 0 评论 0原文

我公司的主要软件包包括一个在启动时加载的庞大配置库。此配置库包含一些强制设置,如果未提供(通过命令行参数),则会导致整个应用程序退出。

对于我们的用户来说,这从来都不是问题,他们通过脚本启动软件,这些脚本会自动提供所需的命令行参数。但有时在调试我们的软件时,我们开发人员会忘记在 Visual Studio 的调试选项中指定必要的参数;然后,收到消息配置规范无效 - 缺少必需的参数 X、Y 和 Z - 正在关闭(当然,我是在解释),这非常烦人。

这其实没什么大不了的,只是一种烦恼。尽管如此,我还是觉得有必要整理一些表格来让这个过程少一点痛苦;它通知用户缺少哪些参数,并允许他/她直接在表单上指定这些参数的值,而无需重新启动应用程序。

我的意图是好的(我认为?),但似乎我无法让这个解决方案真正发挥作用。问题是,在我启动缺少设置的软件后,会弹出表单并按预期提示我;但在我输入所需的参数并且应用程序“真正”启动后,我收到此 InvalidOperationException

SetCompatibleTextRenderingDefault 必须 在第一个之前被调用 IWin32Window对象被创建在 应用程序。

我想我明白这里发生了什么:我正在处理的 VB.NET 项目正在“在幕后”做这样的事情*:

Sub Main()
    Application.EnableVisualStyles()
    Application.SetCompatibleTextRenderingDefault(False)
    Application.Run(New MainForm)
End Sub

SetCompatibleTextRenderingDefault 的调用显然会抛出异常,因为表单在执行之前已经创建并显示。

有什么办法解决这个问题吗?对于这个问题,是否有一个我没有想到的更“正确”的解决方案(即,我根本不应该尝试通过表单收集用户输入)?

*这是根据我在 C# WinForms 项目中看到的内容的最佳猜测。奇怪的是,除非我遗漏了什么,否则 VB.NET WinForms 项目似乎完全向开发人员隐藏了这一点。

My company's main software package includes a hefty configuration library which loads on startup. This config library includes some mandatory settings which, if not supplied (via command line arguments), cause the entire application to exit.

This has never been an issue for our users, who launch the software via scripts which have the needed command line arguments supplied automatically. But sometimes when debugging our software we developers forget to specify the necessary arguments in Visual Studio's debug options; it's then very annoying to be greeted with the message Config specification invalid -- missing required parameters X, Y, and Z -- shutting down (I'm paraphrasing, of course).

It's not really a big deal, just an annoyance. Still, I felt it worthwhile to throw together a little form to make this process a little less painful; it notifies the user which parameters are missing and allows him/her to specify values for those parameters directly on the form, without having to restart the application.

My intentions were good (I think?), but it seems I can't get this solution to actually work. The problem is that after I've launched our software with missing settings, the form pops up and prompts me as expected; but after I've entered the required parameters and it's time for the application to "really" start, I get this InvalidOperationException:

SetCompatibleTextRenderingDefault must
be called before the first
IWin32Window object is created in the
application.

I think I understand what's going on here: the VB.NET project I'm working on is doing something like this "behind the scenes"*:

Sub Main()
    Application.EnableVisualStyles()
    Application.SetCompatibleTextRenderingDefault(False)
    Application.Run(New MainForm)
End Sub

That call to SetCompatibleTextRenderingDefault is, apparently, throwing an exception because a form was already created and displayed prior to its execution.

Is there any way around this? Is there perhaps a more "proper" solution to this problem that I'm not thinking of (i.e., should I not be trying to collect user input via a form at all)?

*This is a best guess based on what I've seen in C# WinForms projects. Strangely, unless I'm missing something, it seems that VB.NET WinForms projects completely hide this from the developer.

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

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

发布评论

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

评论(2

可是我不能没有你 2024-10-15 17:02:34

请确保您已关闭应用程序框架选项并选择 Sub Main 作为启动方法。让它看起来类似于这样:

Sub Main(ByVal args() As String)
    Application.EnableVisualStyles()
    Application.SetCompatibleTextRenderingDefault(False)
    If args.Length = 0 Then
        Using dlg As New OptionsDialog
            If dlg.ShowDialog <> DialogResult.OK Then Return
            '' Use dlg result...
        End Using
    End If
    Application.Run(New MainForm)
End Sub

Do make sure that you have the application framework option turned off and Sub Main selected as the starting method. Make it look similar to this:

Sub Main(ByVal args() As String)
    Application.EnableVisualStyles()
    Application.SetCompatibleTextRenderingDefault(False)
    If args.Length = 0 Then
        Using dlg As New OptionsDialog
            If dlg.ShowDialog <> DialogResult.OK Then Return
            '' Use dlg result...
        End Using
    End If
    Application.Run(New MainForm)
End Sub
゛时过境迁 2024-10-15 17:02:34

也许您可以使用静态 Debugger.IsAttached< /code>(或者甚至是 #DEBUG 指令)在程序的“main”函数中将某些输入文件(例如 XML 文件)输入到解析后的 args 集合中?

Perhaps you could use the static Debugger.IsAttached (or even a #DEBUG directive) in your program's "main" function that feeds in some input file (say an XML file) into your parsed args collection instead?

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