英文:
Visual Studio 2022 gives error "'Site' must not be null in order to access a required service." when trying to add menu item
问题
我有一个 contextMenuStrip
,经常(不总是,但我还没有注意到可重复的模式)在尝试在“Type Here”字段中键入时出现错误对话框:
> 必须不为 null 以访问所需的服务。
当我点击光标进入字段时,它会立即发生。
有人知道是什么原因吗?
英文:
I've got a contextMenuStrip
and often (not always, but I haven't noticed a repeatable pattern yet) I get the error dialog:
> 'Site' must not be null in order to access a required service.
when trying to type in the "Type Here" field. It happens the second I click the cursor into the field.
Anyone have any idea what's causing this?
答案1
得分: 1
以下是翻译好的部分:
这让问题对我来说不再存在。我不知道是哪个步骤解决了问题,但现在似乎正常工作了。
首先,设计师所做的只是为您编写[FormName].Designer.cs
页面。您可以自己添加子项的条目,VS似乎对此没有问题。我开始做这个,看了一个以前的项目,该项目拥有菜单子项(在VS22中完美工作),作为一个模型。
- 在您的设计文件底部的对象定义中,添加菜单项的声明。
- 在隐藏部分,在实例化中,添加一行或多行以创建您的菜单项。
- 此时我注意到较旧的、正常工作的项目中,所有的实例化都以
this
为前缀(即this.LogFileToolStripMenuItem=new ToolStripMenuItem
),对于需要子菜单项的第二个菜单项,我添加了this
,然后重新进入设计师,一切都正常工作了(而且继续如此,没有错误消息)。我不知道这其中哪一部分解决了问题,但当我回到设计文件查看如果删除了this
会发生什么时,设计师编辑器已经自动去掉了this
,但一切仍然正常。 - 您需要为工具栏菜单项设置三个属性。您可能可以复制其中一个现有的菜单项,然后根据您的新
toolstripmenuitem
进行更改。 - 您需要做的最后一件事是将子菜单添加到父菜单中。在父菜单的属性定义部分,添加一个
AddRange
行:
cmsLogging.DropDownItems.AddRange(new ToolStripItem[] { cmsLogging_LogFile, cmsLogging_ShowLogFile, cmsLogging_ShowLogFileDirectory });
在此之后,重新启动表单设计器,菜单项就会出现,一切都看起来正常。作为奖励,错误消息也消失了。这对我有效。
英文:
This made the problem go away for me. I do not know which step did it, but it seems to be working normally now.
First, all the designer does is write the [FormName].Designer.cs
page for you. You can always put the entries in for the subitems in yourself and VS seems to be fine with that. I started this, looking at an older project that had menu subitems (and worked perfectly in VS22) as a model.
- In the object definitions at the bottom of your Designer file, add the declaration for the menu item(s).
- In the hidden section, in the instantiations, add a line(s) to create your menu item(s).
- It was at this point I noticed the older, working project all the instantiations were prefixed with
this
(i.e.this.LogFileToolStripMenuItem=new ToolStripMenuItem
) and on a second menu item that needed submenu items I added thethis
and went back into the designer and everything worked normally (and continued to do so, no error message) from there on out. I don't know which part of this fixed it but when I went back into the designer file to see what happened if I removed thethis
the designer editor had already stripped thethis
out, but everything still worked. - You need to set the three properties for a tool strip Menu Item. You can probably copy one of the existing ones and just change things to fit your new
toolstripmenuitem
. - The last thing you need to do is add the submenus to the parent. In the property definition section for the parent, add an
AddRange
line:
cmsLogging.DropDownItems.AddRange(new ToolStripItem[] { cmsLogging_LogFile, cmsLogging_ShowLogFile, cmsLogging_ShowLogFileDirectory });
After this and restarting the form designer the menu items were present and everything looked normal. As a bonus the error message is gone away. It worked for me.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论