Visual Studio 2008警告关于Designer生成的代码的更改

Nan*_*ada 3 c# designer visual-studio-designer visual-studio-2008

这个问题有点轶事,但对我来说仍然很有趣; 我想知道为什么Visual Studio 2008不喜欢以下常量使用:

public class Service101 : ServiceBase
{
    /// <remarks>
    /// Shown at Start -> Settings -> Control Panel -> Administrative Tools -> Services
    /// </remarks>
    internal const string SERVICE_NAME = "WinSvc101";
    /// <remarks>
    /// Shown at Start -> Settings -> Control Panel -> Administrative Tools -> Services
    /// </remarks>
    internal const string DISPLAY_NAME = "Windows Service 101";
    /// <summary>
    /// Public constructor for Service101.
    /// </summary>      
    public Service101()
    {
        InitializeComponent();
    }

    private void InitializeComponent()
    {
        this.ServiceName = Service101.SERVICE_NAME;
        this.EventLog.Source = Service101.DISPLAY_NAME;
        this.EventLog.Log = "Application";

        if (!EventLog.SourceExists(Service101.DISPLAY_NAME))
        {
            EventLog.CreateEventSource(Service101.DISPLAY_NAME, "Application");
        }
    }
    #region Events
    /// <summary>
    /// Dispose of objects that need it here.
    /// </summary>
    /// <param name="disposing">Whether or not disposing is going on.</param>
    protected override void Dispose(bool disposing)
    {
        // TODO: Add cleanup code here (if required)
        base.Dispose(disposing);
    }
Run Code Online (Sandbox Code Playgroud)

因为它在设计时显示以下警告:

Warning 1   The designer cannot process the code at line 68: 

if (!EventLog.SourceExists(DISPLAY_NAME))
{
    EventLog.CreateEventSource(DISPLAY_NAME, "Application");
}

The code within the method 'InitializeComponent' is generated by the designer and should not be manually modified.  Please remove any changes and try opening the designer again.   E:\Proyectos\beanstalk\dotnetfx\trunk\WinSvc101\WinSvc101\Service101.cs 69  0   
Run Code Online (Sandbox Code Playgroud)

任何评论都会非常感激.非常感谢提前.

Joh*_*ers 5

它实际上告诉过你.该代码由设计者生成.设计师需要它离开它的方式.不要更改该代码,除非您希望设计人员使用它做一些不愉快的事情.


在视觉设计器中看到的与它生成的代码之间存在某种平衡.

  1. 你从一个空的设计表面开始,所以没有生成代码
  2. 您将某些东西拖到设计图面上.设计者生成创建它所需的代码.
  3. 您可以设置该对象的属性,并且设计器会生成用于设置指定属性的代码.
  4. 你保存并关闭
  5. 您在设计器中重新打开文档.设计师必须弄清楚要在设计表面上显示的内容.它读取它生成的代码,并且因为它知道代码是由它自己生成的,所以它知道代码在设计表面方面意味着什么.
  6. 下次更改或保存时,它将重新生成代码.

现在,假设您对生成的代码进行了一些修改.除非您以与设计师完全相同的方式进行更改,否则它将无法识别更改.您的更改不会显示在设计图面上.下次更改或保存时,设计人员将在不进行更改的情况下重新生成代码.

因此,如果您不想丢失对生成代码的更改,则不要对生成的代码进行任何更改.