Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Re-Link Xaml and Code Behind in Visual Studio Solution

I have XAML file say SomePage.xaml.

I was making a mistake by refactored it's code behind class from SomePage to CardsPage class.

Now XAML file doesn't link with the code behind anymore.

Separate File

How to link these files together again?

like image 403
Ask Too Much Avatar asked Mar 08 '23 22:03

Ask Too Much


2 Answers

If you open the project file that includes your CardsPage, you can link them back together again.

You should have 2 sections in there, one should be like this:

<Page Include="CardsPage.xaml">
  <Generator>MSBuild:Compile</Generator>
  <SubType>Designer</SubType>
</Page>

The other section should look like this:

<Compile Include="CardsPage.xaml.cs">
  <DependentUpon>CardsPage.xaml</DependentUpon>
</Compile>

The DependentUpon is important, it will nest the file in the IDE below the xaml.
Note: In your screenshot, the CardsPage looks like it was renamed to CardsPage.cs, I would make sure that you rename it back to CardsPage.xaml.cs.

Hope that helps, I sadly don't know any other way than fiddling in the project file to make it work again.

like image 82
huserben Avatar answered Mar 19 '23 12:03

huserben


I have some problems when I refactor the code behind with the XAML(renaming for example) and I find a small extension to "relink" files together.

https://marketplace.visualstudio.com/items?itemName=MadsKristensen.FileNesting

It's easier than modifying the csproj and less dangerous.

See Example Below

Step 1

Step 2

[[2]

Result

like image 32
alexdess Avatar answered Mar 19 '23 13:03

alexdess