我在將大型(遺留)表單拆分爲部分類之後,今天遇到了此問題,同時向其中添加了新的部分。 儘管這是一個古老的問題,但我發帖的原因很多,答案都不正確。具體而言,他們中的許多人聲稱,如果沒有雙擊,Visual Studio無法生成這些內容。我想確認在某些情況下,VS 確實是。
在我的特殊情況下,我向選項卡控件添加了一個新選項卡,並將新選項卡事件和代碼放入新類中。最初,我已將此代碼(包括事件)置於主窗體的末尾,其中可能與有關。在移動代碼之後,如果我將控件添加到主窗體的任何部分(例如單獨的選項卡),VS會爲我的新分部類中的3個事件生成空事件處理程序,儘管此時具有完整的事件處理程序類。
我現在唯一的解決方案是在發生問題時從主窗體代碼的底部刪除空處理程序。解決方案清理不會刪除這些空白處理程序。
因爲所有3個每次都一致地生成,並且由於它們在添加測試控件時隱藏在主窗體後面的選項卡上(所以沒有雙擊的機會,當然沒有雙擊所有3的機會),我可以放心地說,這是一個Visual Studio錯誤(VS 2013在我的情況下)。
UPDATE 24/9/15:在厭惡刪除處理程序後,我做了一些更多的搜索,並找到了解決方案。下面是來自MS論壇的mustiy解決方案的文本(https://social.msdn.microsoft.com/Forums/windows/en-US/79910eaa-84e7-472d-95ee-4b8ddfbf99f0/multiple-partial-class-files-cause-problems-with-forms-designer?forum=winforms&prof=required)。雖然點擊創建事件仍然會在錯誤的類中創建它們,但是在移動它們之後,它們可以正常工作。它還可以防止創建無效事件處理程序。
i had same problem and came accross your post, but i wasn't sattisfied. It had to be a solution for this because VS IDE does it.
Here is the solution :
Open the .csproj project file with notepad and let's say for a Form1.cs file you will see a configuration like
<Compile Include="Form1.cs">
<SubType>Form</SubType>
</Compile>
<Compile Include="Form1.Designer.cs">
<DependentUpon>Form1.cs</DependentUpon>
</Compile>
If you create a new file called Form1Events.cs and put another partial class of Form1 and move the events to the new partial class. In the new .csproj file you the new file like this below
<Compile Include="Form1Events.cs">
<SubType>Form</SubType>
</Compile>
Something is missing you have to tell the VS IDE that this Form1Events.cs file is dependent to Form1.cs. As the IDE do this for Form1.Designer.cs file make the same with Form1Events.cs and put a "DependentUpon" tag into Form1Events.cs. Last the .csproj file look like this below
<Compile Include="Form1.cs">
<SubType>Form</SubType>
</Compile>
<Compile Include="Form1.Designer.cs">
<DependentUpon>Form1.cs</DependentUpon>
</Compile>
<Compile Include="Form1Events.cs">
<DependentUpon>Form1.cs</DependentUpon>
<SubType>Form</SubType>
</Compile>
And if you save the .csproj file the IDE reloads the project and on the Solution exlorer tree you see that the new partial class is put below Form1.cs file. And if you open the designer and double click a previous decalred and moved event it will navigate to the new file.
VS不會任意創建存根。在這種情況發生之前你是否雙擊控件? –
如果您使用表單設計器,如果您(雙擊)單擊設計器中的按鈕,它將自動創建這些表單。 – yoozer8
首先,如果我雙擊已經定義了事件處理程序的控件,它將帶我到那個事件處理程序,而不是創建一個新的。無論如何,這並不重要,因爲我沒有點擊控件。 –