最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】C#的VS中打开Form1.cs出错:类 Form1 可以进行设计,但不是文件中的第一个类。

C# crifan 12975浏览 0评论

【问题】

拿到一个VS2010的项目,C#代码。

用VS打开后,双击Form1.cs去打开,结果出错:

若要在加载设计器前避免可能发生的数据丢失,必须纠正以下错误

类 Form1 可以进行设计,但不是文件中的第一个类。Visual Studio 要求设计器使用文件中的第一个类。移动类代码使之成为文件中的第一个类,然后尝试重新加载设计器。

此错误的实例(1)

1。
隐藏调用堆栈

在 System.ComponentModel.Design.Serialization.CodeDomDesignerLoader.EnsureDocument(IDesignerSerializationManager manager)
在 System.ComponentModel.Design.Serialization.CodeDomDesignerLoader.PerformLoad(IDesignerSerializationManager manager)
在 Microsoft.VisualStudio.Design.Serialization.CodeDom.VSCodeDomDesignerLoader.PerformLoad(IDesignerSerializationManager serializationManager)
在 Microsoft.VisualStudio.Design.Serialization.CodeDom.VSCodeDomDesignerLoader.

DeferredLoadHandler.Microsoft.VisualStudio.TextManager.Interop.IVsTextBufferDataEvents.OnLoadCompleted(Int32 fReload)

有关此错误的帮助

MSDN 帮助

有关此错误的论坛文章

在 MSDN 论坛中搜索与此错误相关的文章

如图:

form1 cs not first class move to first

【解决过程】

1.参考:

类可以进行设计,但不是文件中的第一个类

去移动代码,使得form1为第一个类:

form1 cs check code

变成:

......

namespace WindowsFormsApplication3
{
    public partial class Form1 : Form
    {
        public Form1(string sss)
        {
            InitializeComponent();
            //this.textBox1.Text = sss;

        }

        public Form1()
        {
            InitializeComponent();
            Class1 C = new Class1();
            C.visit(textBox2);


        }
        ......
    }


    public class a
    {
        public void s()
        {
            ......
        }
    }
}

然后再去双击Form1.cs,看看效果。

 

2.貌似是可以了,

但是又出现其他错误:

设计器无法处理第 101 行的代码: for (int i = 0; i < s.FrameCount; i++) { s.GetFrame(i); } 方法“InitializeComponent”内的代码由设计器生成,不应手动修改。请移除任何更改,然后尝试重新打开设计器。

此错误的实例(1)

1。

WindowsFormsApplication3 Form1.Designer.cs 行:101 列:1

显示调用堆栈

在 Microsoft.VisualStudio.Design.Serialization.CodeDom.XML.CodeDomXmlProcessor.CreateQuoteExpression(XmlElementData xmlElement)

在 Microsoft.VisualStudio.Design.Serialization.CodeDom.XML.CodeDomXmlProcessor.XmlElementData.get_CodeDomElement()

在 Microsoft.VisualStudio.Design.Serialization.CodeDom.XML.CodeDomXmlProcessor.EndElement(String prefix, String name, String urn)

在 Microsoft.VisualStudio.Design.Serialization.CodeDom.XML.CodeDomXmlProcessor.Parse(XmlReader reader)

在 Microsoft.VisualStudio.Design.Serialization.CodeDom.XML.CodeDomXmlProcessor.ParseXml(String xmlStream, CodeStatementCollection statementCollection, String fileName, String methodName)

在 Microsoft.VisualStudio.Design.Serialization.CodeDom.VSCodeDomParser.OnMethodPopulateStatements(Object sender, EventArgs e)

在 System.CodeDom.CodeMemberMethod.get_Statements()

在 System.ComponentModel.Design.Serialization.TypeCodeDomSerializer.Deserialize(IDesignerSerializationManager manager, CodeTypeDeclaration declaration)

在 System.ComponentModel.Design.Serialization.CodeDomDesignerLoader.PerformLoad(IDesignerSerializationManager manager)

在 Microsoft.VisualStudio.Design.Serialization.CodeDom.VSCodeDomDesignerLoader.PerformLoad(IDesignerSerializationManager serializationManager)

在 System.ComponentModel.Design.Serialization.BasicDesignerLoader.BeginLoad(IDesignerLoaderHost host)

有关此错误的帮助

未能找到与此错误相关的帮助主题。 检查 Windows 窗体设计时错误列表

有关此错误的论坛文章

在 MSDN 论坛中搜索与此错误相关的文章

然后点击到对应代码去看看:

designer can not handle so click jump to code

也看不太懂。

注释掉算了:

        private void InitializeComponent()
        {
            //StackTrace a1111 = GetStackTrace(Thread.CurrentThread);
            //System.Diagnostics.StackTrace s = new System.Diagnostics.StackTrace();

            //for (int i = 0; i < s.FrameCount; i++)
            //{
            //    s.GetFrame(i);
            //}
            //a a = new a();
            //a.s();
            this.textBox1 = new System.Windows.Forms.TextBox();
            this.textBox2 = new System.Windows.Forms.TextBox();
            this.textBox3 = new System.Windows.Forms.TextBox();
            this.SuspendLayout();
            ......

看看效果,最后终于可以打开窗口设计器了:

finnally can open win form

 

【总结】

别人的代码,够乱。。。

转载请注明:在路上 » 【已解决】C#的VS中打开Form1.cs出错:类 Form1 可以进行设计,但不是文件中的第一个类。

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
82 queries in 0.159 seconds, using 22.14MB memory