![](/img/trans.png)
[英]Why does C# designer-generated code (like Form1.designer.cs) play havoc with Subversion?
[英]Visual Studio 2008 Warning About Changes to Designer-Generated Code
這個問題有點軼事,但對我來說仍然很有趣; 我想知道為什么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
任何評論都會非常感激。 非常感謝提前。
它實際上告訴過你。 該代碼由設計者生成。 設計師需要它離開它的方式。 不要更改該代碼,除非您希望設計人員使用它做一些不愉快的事情。
在視覺設計器中看到的與它生成的代碼之間存在某種平衡。
現在,假設您對生成的代碼進行了一些修改。 除非您以與設計師完全相同的方式進行更改,否則它將無法識別更改。 您的更改不會顯示在設計圖面上。 下次更改或保存時,設計人員將在不進行更改的情況下重新生成代碼。
因此,如果您不想丟失對生成代碼的更改,則不要對生成的代碼進行任何更改。
將代碼添加到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");
}
}
對我來說似乎很清楚。 它告訴你,你已經修改了自動生成的代碼,你不應該這樣做。
在最壞的情況下,您可能會丟失您的更改。 在最好的情況下,它會發現一些意外的代碼,並且它不會改變任何內容,因此您不會丟失更改。 但它也無法理解你的代碼。 您應該將常量放在任何其他位置。
稍微偏離了這一點,但我真的不明白你為什么要使用常量(以及公共的那些),無論如何。 你不能這樣做嗎?
private void InitializeComponent()
{
this.ServiceName = "WinSvc101";
this.EventLog.Source = "Windows Service 101";
// ....
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.