gpt4 book ai didi

c# - Visual Studio 2010 设计器生成无效代码

转载 作者:行者123 更新时间:2023-11-30 14:14:34 25 4
gpt4 key购买 nike

这对我来说是第一次,我以为我完全失去了理智。我有一个简单的 winform 应用程序,我向其中添加了一个 datagridview。我为 dgv 设置数据源,使用我在其他六个项目中使用的相同源,并按照我想要的方式配置网格。此时除了网格外,窗体上没有其他任何东西,项目现在包含一个数据集、一个绑定(bind)源和一个表适配器。伟大的。然而,设计者在这样做时生成的代码是无效的,并导致编译错误,告诉我数据集表适配器对象和数据集本身不存在。

如果我进入编译错误所在的设计器,我会看到以下几行:

this.tILEDataSet = new ImageEdit.TILEDataSet();
this.logosTableAdapter = new ImageEdit.TILEDataSetTableAdapters.LogosTableAdapter();

ImageEdit 是我添加了这个绑定(bind)控件的类。如果我删除“ImageEdit.”,代码将编译并完美运行。当然,由于这是设计器生成的文件,一旦我进行任何其他更改,它就会恢复为有问题的代码。

我用三个不同的项目做了三次,两次是从头开始使用 visual studio 的一个新实例。

导致这种情况发生的原因可能是什么?除了手动编辑设计器文件(我从来不喜欢这样做)之外,是否有修复方法?

我什么都没改,一切都是设计师生成的。命名空间和类都创建为 ImageEdit。设计器包含以下定义: 私有(private) TILEDataSet tILEDataSet; 私有(private) TILEDataSetTableAdapters.LogosTableAdapter logosTableAdapter;

设计器类定义确实派生自 global::System.ComponentModel.Component:

public partial class LogosTableAdapter : global::System.ComponentModel.Component {
.
.
.

再次感谢您的任何见解。

最佳答案

我怀疑您的解决方案中有多个名为 ImageEdit 的成员,无论它们是 namespace 、类还是其他成员。在微软的Guidelines for Names ,特别是 Names of Namespaces他们推荐:

Do prefix namespace names with a company name to prevent namespaces from different companies from having the same name and prefix.

Do not use the same name for a namespace and a type in that namespace. For example, do not use Debug for a namespace name and also provide a class named Debug in the same namespace. Several compilers require such types to be fully qualified.

Do not give the same name to types in namespaces within a single application model.
For example, if you were writing a library of special controls to be used by Windows forms application developers, you should not introduce a type named Checkbox because a type with this name already exists for the application model (CheckBox).

仅第二点就可以解决您的问题。使用第一点,将公司名称作为命名空间的一部分,将使代码生成器为您的对象提供更简洁的名称,例如:

this.tILEDataSet = new MyCompany.ImageEdit.TILEDataSet();
this.logosTableAdapter = new MyCompany.ImageEdit.TILEDataSetTableAdapters.LogosTableAdapter();

关于c# - Visual Studio 2010 设计器生成无效代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11782732/

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