以下方法之间的调用不明确:Identical.NameSpace.InitializeComponent()和Identical.NameSpace.InitializeComponent()

 yuan00911 发布于 2023-02-12 14:57

好吧,我怀疑这可能是Visual Studio的事情,但必须有一些原因.我从默认项目列表中创建了一个ListBox(右键单击项目,或项目中的文件夹 - >添加 - >新项目 - > Xaml ListBox).我立刻得到了一条带有错误的红色波浪线:

"错误2以下方法或属性之间的调用不明确:'Identical.NameSpace.ListBox1.InitializeComponent()'和'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就行了,我想继续前进.但我的问题是:为什么此代码会自动生成此错误?我希望自动生成的任何东西都能够构建和编译,而无需触摸项目或任何代码.对于您可以添加的几乎所有其他toobox项目,情况就是如此.那么为什么要使用内置错误生成此代码呢?我们应该找到一些方法来完成这项工作吗?这段代码仅仅是一个建议,由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"
    >
    Item 1
    Item 2
    Item 3
< /ListBox>

ListBox1.g.cs:

namespace Identical.Namespace
{
    /// 
    /// ListBox1
    /// 
    [System.CodeDom.Compiler.GeneratedCodeAttribute("PresentationBuildTasks", "4.0.0.0")]
    public partial class ListBox1 : System.Windows.Controls.ListBox, System.Windows.Markup.IComponentConnector {

        private bool _contentLoaded;

        /// 
        /// InitializeComponent
        /// 
        [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
{
    /// 
    /// ListBox1 class
    /// 
    public partial class ListBox1 : ListBox
    {
        /// 
        /// Initializes a new instance of the ListBox1 class
        /// 
        public ListBox1()
        {
            this.InitializeComponent();
        }
    }
}

而已.这是完全处于自动生成状态的代码,但我放入xaml.cs文件的注释除外.

我搜索过这个网站和互联网,但似乎没有人解释过这种行为.我可能会删除designer.cs代码并继续.但是,如果有人知道为什么这首先出现在这里,或者它确实是Visual Studio 2010专业版中的错误,我真的很想知道.

2 个回答
  • 您似乎已InitializeComponent在类中的两个位置声明了该方法,可能在每个分部类中都有一个.尝试InitializeComponent在Visual Studio中搜索所有文件,我猜测结果将列出声明它的两个位置.删除一个,错误将消失.


    更新>>>

    我不确定你在这里期待什么样的答案......显然,如果你没有添加其中一个InitializeComponent方法定义,那么visual Studio就有一个bug.我非常怀疑除了这是一个错误之外,还有任何逻辑上的原因.

    更新2 >>>

    我在Microsoft Connect网站上查看了任何现有的报告错误,但是找不到任何错误...如果你想向他们报告,我已经把链接留在了这里.

    2023-02-12 14:59 回答
  • 在控件之间复制我的XAML时遇到了这个问题.我只需要更改我的x:Class ="mynamespace",其中mynamespace是项目的正确命名空间.重新编译,一切都恢复正常.

    2023-02-12 14:59 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有