Nesting a file in VS project

How to nest a file under another file to keep them close to each other

Nesting a file in VS project

Here is just a quick post from about something I do often, nesting a file under another file.

The scenario

Lets say I'm programming a Xamarin.Forms app and I have a View and a ViewModel and they are in their separate folders or even in another project.

That setup is just fine if you will be reusing the ViewModels with other Views that meight be in another solution even.

But usually a ViewModel will only be used for one View, and when that is so I really like to have the ViewModel and View in the same place.

How?

If you have a .net standard 2.0 project, with your Views and ViewModel in the Views folder (can be where ever you like), you can just edit your project and add the following code to the project file.

<ItemGroup Label="ViewsAndViewModelNesting">
 <Compile Update="Views\MainPageViewModel.cs" DependentUpon="MainPage.xaml" />
</ItemGroup>

The end result will be like the following image with the ViewModel nested below the View.
NestingBlog

p.s
Note that sometimes you will need to unload and reload your project to see the effect of nesting take place.