gpt4 book ai didi

c# - 错误 : The service System. Windows.Forms.Design.IEventHandlerService 已存在于服务容器中

转载 作者:数据小太阳 更新时间:2023-10-29 01:42:56 28 4
gpt4 key购买 nike

我正在开发基于 Windows 窗体模板的 Windows 应用程序。我正在使用 .NET 3.5 版本。在此应用程序中,目标是可以从 App.Config 文件管理不同表单的所有视觉设置(背景颜色、不同按钮的背景颜色等...)。

所以基本上,我有一个“FormBase”类,我的所有表单都继承了它,并且这个类包含如下代码:

public class FormBase : Form
{
protected override void OnLoad(EventArgs e)
{
BackColor = Color.FromName(ConfigurationManager.AppSettings["backColor"]);

foreach (var item in this.Controls)
{
if (item is Button)
{
((Button)item).BackColor = Color.FromName(ConfigurationManager.AppSettings["buttonBackground"]);
((Button)item).ForeColor = Color.FromName(ConfigurationManager.AppSettings["buttonText"]);
}
if (item is ...)
{
//some other code
}
}
}
}

然后我有我的 App.Config 文件,其中包含如下代码:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="buttonText" value="White"/>
<add key="buttonBackground" value="Red"/>
<add key="backColor" value="White"/>
<add key="textColor" value="Red"/>
</appSettings>
</configuration>

现在,在我所有表格的声明中,我都有一行

public partial class Form1 : FormBase

我的问题是,当我运行该应用程序时,它运行良好并且可以正常工作,App.Config 文件中的不同颜色是我表单上显示的颜色。但是,当我在 Visual Studio 中查看设计器而不运行应用程序时,设计器无法显示表单的外观,我收到以下错误

The service System.Windows.Forms.Design.IEventHandlerService already exists in the service container. Parameter name: serviceType

我不知道如何解决这个问题。这不是什么大问题,因为该应用程序无论如何都能正常运行,但这让我很困扰,我想知道发生了什么

最佳答案

我自己也遇到了这个问题。根据另一个网页,可以通过关闭 visual studio 并删除您的 obj 文件夹,然后重新打开 visual studio 并重新构建项目来修复此错误。

这是我阅读的页面。 http://www.csharp411.com/ieventhandlerservice-already-exists-in-the-service-container/

他们说也要删除 bin 文件夹,但我发现我不必那样做。希望这对您有所帮助!

关于c# - 错误 : The service System. Windows.Forms.Design.IEventHandlerService 已存在于服务容器中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24821737/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com