代码之家  ›  专栏  ›  技术社区  ›  Nano Taboada

Visual Studio 2008关于设计器生成的代码更改的警告

  •  3
  • Nano Taboada  · 技术社区  · 17 年前

    这个问题有点轶事,但对我来说仍然很有趣;我想知道为什么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);
        }
    

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

    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   
    

    任何评论都将不胜感激。提前多谢。

    4 回复  |  直到 17 年前
        1
  •  3
  •   Andrew Hare    17 年前

    当您将代码添加到 InitializeComponent() . 改为尝试类似的操作:

    public Service101()
    {
        InitializeComponent();
        this.createEventSource();
    }
    
    private void InitializeComponent()
    {
        this.ServiceName = SERVICE_NAME;
        this.EventLog.Source = DISPLAY_NAME;
        this.EventLog.Log = "Application";
    }
    
    void createEventSource()
    {
        if (!EventLog.SourceExists(DISPLAY_NAME))
        {
            EventLog.CreateEventSource(DISPLAY_NAME, "Application");
        }
    }
    
        2
  •  5
  •   John Saunders    17 年前

    它实际上告诉了你。该代码由设计器生成。设计师需要它保持原样。不要更改该代码,除非您希望设计器对其进行不愉快的操作。


    在可视化设计器中看到的内容和它生成的代码之间有一种平衡。

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

    现在,假设您对生成的代码进行了一些修改。除非你在 确切地 就像设计师所做的那样,它不会识别出变化。您的更改不会显示在设计图面上。下次发生更改或保存时,设计器将重新生成代码 没有你的改变 .

    因此,如果您不想丢失对生成代码的更改,那么就不要对生成的代码进行任何更改。

        3
  •  3
  •   raven    17 年前

    我觉得很清楚。它告诉你你已经修改了自动生成的代码,你不应该这样做。

    在最坏的情况下,您可能会丢失更改。最好的情况是,它会找到一些意外的代码,并且不会更改任何内容,这样您就不会丢失更改。但它也无法理解您的代码。您应该将常量放在任何其他位置。

        4
  •  1
  •   Dan Diplo    17 年前

    稍微偏离点,但我不明白为什么你要使用常量(和公共常量)。你不能就这么做吗?

    private void InitializeComponent()
    {
        this.ServiceName = "WinSvc101";
        this.EventLog.Source = "Windows Service 101";
        // ....
    }