开发者

WinForm partial classes

开发者 https://www.devze.com 2022-12-24 16:13 出处:网络
I have a WinForm project that contains a form called MainUI.You can see that the automatically generated p开发者_开发问答artial class shows up as a node under MainUI.cs. Is there a way to \"move\" my

I have a WinForm project that contains a form called MainUI. You can see that the automatically generated p开发者_开发问答artial class shows up as a node under MainUI.cs. Is there a way to "move" my self created partial class MainUI.Other.cs under MainUI.cs so that it'll show as another node?

WinForm partial classes


Close the solution in Visual Studio, and open your .csproj file in a text editor. Find MainUI.Other.cs, and add the following XML element:

<Compile Include="MainUI.Other.cs">
  <SubType>Form</SubType>
  <DependentUpon>MainUI.cs</DependentUpon>  <!-- this is the magic incantation -->
</Compile>

Reopen the solution in Visual Studio and enjoy subnodular goodness.

That said, you may want to reconsider whether this is a good idea. The reason the .designer.cs file is displayed as a subnode is because you won't normally need or want to open it, because it contains generated code which you'd normally view or edit through the designer. Whereas a partial class file will contain your code, that you'll want to edit and view; it may be confusing to maintenance programmers if the file is not easily visible in Solution Explorer. However, only you can know what's right for your project -- just something to bear in mind!


Yes, this is possible, but you will have to hand edit the project file.

In the project file (open it with the XML Editor) locate the file listing item group. In my example, I left the form as "Form1.cs". Add the child element "<DependentUpon>" to your extended class as per the example below:

 <Compile Include="Form1.cs">
      <SubType>Form</SubType>
    </Compile>
    <Compile Include="Form1.Designer.cs">
      <DependentUpon>Form1.cs</DependentUpon>
    </Compile>
    <Compile Include="Form1.Designer.Other.cs">
      <DependentUpon>Form1.cs</DependentUpon>
      <SubType>Form</SubType>
    </Compile>

Typically though you wouldn't want any non-generated code to be hidden as a child node though. My normal practice is to create a folder in the project called "Partial Classes" and add them all in the same location.


You can modify the project source file to group the related files. In the project source file, find ItemGroup element which contains MainUI.cs and add an entry for MainUI.Others.cs

Here a blog post showing how to do it in details. Group/nest source code files


Just to add to @itowlson's answer: if you're getting an error such as "Duplicate 'Compile' items were included." when compiling, that's probably because the files you're telling it to include are already included using a wild card.

The solution is to remove then add them to the compile config like so:

</Project>
  <ItemGroup>
    <Compile Remove="MainUI.Other1.cs" />
    <Compile Remove="MainUI.Other2.cs" />
  </ItemGroup>
  <ItemGroup>
    <Compile Include="MainUI.Other1.cs">
      <DependentUpon>MainUI.cs</DependentUpon>
    </Compile>
    <Compile Include="MainUI.Other2.cs">
      <DependentUpon>MainUI.cs</DependentUpon>
    </Compile>
  </ItemGroup>
</Project>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号