我该如何将ViewModel文件(一个.cs文件)折叠到其对应的View文件(一个.xaml文件)中,就像这张图片中展示的那样?
我该如何将ViewModel文件(一个.cs文件)折叠到其对应的View文件(一个.xaml文件)中,就像这张图片中展示的那样?
我不知道在 Visual Studio 中有没有这样做的方法,但是您可以在文本编辑器中编辑您的 .csproj 文件。您应该会找到类似于以下内容:
<Page Include="MainWindow.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</Page>
<Compile Include="MainWindow.xaml.cs">
<DependentUpon>MainWindow.xaml</DependentUpon>
<SubType>Code</SubType>
</Compile>
这两个标签可能不会紧挨着出现在您的文件中。重要的部分是<DependantUpon>
标签。
在您的文件中,找到带有include="ViewModel.cs"
属性的标签。在此标签内部添加<DependantUpon>View.xaml</DependantUpon>
作为子元素。现在您的ViewModel.cs文件将始终位于View.xaml文件中。最后,您的文件应该类似于以下代码片段:
<Page Include="View.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Compile Include="ViewModel.cs">
<DependentUpon>View.xaml</DependentUpon>
</Compile>
MainWindowViewModel.cs
被折叠在MainWindow.xaml
中,请将以下行添加到你的csproj文件中。 <ItemGroup>
<Compile Update="**\*ViewModel.cs">
<DependentUpon>$([System.String]::Copy(%(Filename)).Replace('ViewModel', '.xaml'))</DependentUpon>
</Compile>
</ItemGroup>
然后卸载并重新加载项目。现在,如果你添加任何文件名带有ViewModel.cs
后缀的类文件,它将显示在你的.xaml文件下方。