好的,我怀疑这可能是一个 Visual Studio 的问题,但一定有原因。我从默认项列表中创建了一个 ListBox(右键单击项目或项目中的文件夹 -> 添加 -> 新项目 -> Xaml ListBox)。然后我立即得到了一个红色的波浪线错误:
"Error 2 The call is ambiguous between the following methods or properties: 'Identical.NameSpace.ListBox1.InitializeComponent()' and 'Identical.NameSpace.ListBox1.InitializeComponent()' C:\Documents and Settings\ouflak\My Documents\Visual Studio 2010\Projects\Identical\NameSpace\ListBox1.xaml.cs 27"
所有相关的代码都是自动生成的,错误的原因是由于两个自动生成的文件 ListBox1.g.cs 和 ListBox1.designer.cs 冲突,在其中都声明了 public void
InitializeComponent()。在这种情况下,代码无法编译。删除 ListBox1.designer.cs 并继续工作就很简单。 但我的问题是:为什么这段代码会自动生成这个错误?我希望任何自动生成的东西都能够构建和编译而不需要修改项目或代码。对于几乎所有其他可以添加的工具箱项,情况都是如此。那么为什么要生成具有内置错误的代码?我们应该找到某种方法使其正常工作吗?这段代码只是一些建议,需要 IDE 用户/开发人员来处理细节吗?
这是生成的代码: ListBox1.xaml:
< ?xml version="1.0" encoding="utf-8" ? >
< ListBox
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:xc="http://ns.neurospeech.com/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
x:Class="Identical.NameSpace.ListBox1"
>
<sys:String>Item 1</sys:String>
<sys:String>Item 2</sys:String>
<sys:String>Item 3</sys:String>
< /ListBox>
ListBox1.g.cs:
namespace Identical.Namespace
{
/// <summary>
/// ListBox1
/// </summary>
[System.CodeDom.Compiler.GeneratedCodeAttribute("PresentationBuildTasks", "4.0.0.0")]
public partial class ListBox1 : System.Windows.Controls.ListBox, System.Windows.Markup.IComponentConnector {
private bool _contentLoaded;
/// <summary>
/// InitializeComponent
/// </summary>
[System.Diagnostics.DebuggerNonUserCodeAttribute()]
public void InitializeComponent() {
if (_contentLoaded) {
return;
}
_contentLoaded = true;
System.Uri resourceLocater = new System.Uri("/MyProject;component/namespace/listbox1.xaml", System.UriKind.Relative);
#line 1 "..\..\..\namespace\ListBox1.xaml"
System.Windows.Application.LoadComponent(this, resourceLocater);
#line default
#line hidden
}
[System.Diagnostics.DebuggerNonUserCodeAttribute()]
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)]
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Design", "CA1033:InterfaceMethodsShouldBeCallableByChildTypes")]
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Maintainability", "CA1502:AvoidExcessiveComplexity")]
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1800:DoNotCastUnnecessarily")]
void System.Windows.Markup.IComponentConnector.Connect(int connectionId, object target) {
this._contentLoaded = true;
}
}
}
ListBox1.designer.cs:
namespace Identical.NameSpace
{
using System;
public partial class ListBox1 : System.Windows.Controls.ListBox
{
private void InitializeComponent()
{
// Pre Statements...
string string1 = "Item 1";
string string2 = "Item 2";
string string3 = "Item 3";
// Statements...
this.BeginInit();
this.Items.Add(string1);
this.Items.Add(string2);
this.Items.Add(string3);
this.EndInit();
// Post Statements...
}
}
}
最后是ListBox1.xaml.cs(仅修改以防止XML文档和Stylecop警告):
namespace Identical.NameSpace
{
/// <summary>
/// ListBox1 class
/// </summary>
public partial class ListBox1 : ListBox
{
/// <summary>
/// Initializes a new instance of the ListBox1 class
/// </summary>
public ListBox1()
{
this.InitializeComponent();
}
}
}
就是这样,这段代码完全是自动生成的,只有我在xaml.cs文件中添加的注释。
我搜索了这个网站和互联网一段时间,但似乎没有人解释这种行为。 我可能会删除designer.cs代码并继续前进。 但如果有人知道这首先为什么在这里,或者它是否确实是Visual Studio 2010专业版中的错误,我真的很想知道。