8

Instead of excluding it, by mistake, I deleted the .cs code behind file for an Xaml file. Now, I don't know how to add the code behind.

This Window is empty with no UI controls on it. "View Code" is disabled and I can't see the Events (lightning bolt icon) anywhere for this xaml.

Please help.

Josh Lee
  • 171,072
  • 38
  • 269
  • 275
FMFF
  • 1,652
  • 4
  • 32
  • 62
  • 1
    Undo, restore from recycle bin or from version control isn't an option? – Joey Jun 11 '10 at 14:18
  • None of that helped in my case. In the end, I had to delete the Xaml and create one from scratch. – FMFF Jun 11 '10 at 14:30

4 Answers4

16

There are two required steps and one optional:

  1. Create a new .xaml.cs file in the same directory (Right-click project -> Add -> New Item -> Class)
  2. Copy in the boilerplate code from some other .xaml.cs in your project, changing the class name appropriately (eg. copy the "using" directives, class declaration, and constructor including the InitializeComponent call).
  3. (optional) Edit your .csproj file to add a <DependentUpon> element below your <Compile> element for the .xaml.cs file so that it will appear "inside" the .xaml file not simply below it. If you are updating project to be edited with Blend (v4), this third step is required, not optional, in order to use the inspector to add events to controls.

To easily edit the .csproj file:

  1. Right-click the project and select "Unload Project"
  2. Right-click the project node again and select "Edit [projectname].csproj"
  3. Edit the XML, then close the file
  4. Right-click the project node again and select "Reload Project"

If you're using VB.NET, everything the same, just replace "cs" with "vb".

S Wilkinson
  • 162
  • 12
Ray Burns
  • 62,163
  • 12
  • 140
  • 141
  • This also works in VS2019, and if you click "Reload [projectname]" in solution explorer it asks so save and close the csproj file, so no need for Step 3 in 2nd paragraph – Sebastian Oct 21 '19 at 10:57
3

I think all you have to do is create a new class file and name it exactly like your xaml with the cs extension...

test.xaml

test.xaml.cs

works in vb....

ecathell
  • 1,030
  • 13
  • 25
2

As far as I know without Version Control there is no way to revert back if the project has been saved. You should look into putting your code on Version Control.

At this point I would consider recreating your xaml file, copying your old code in, then deleting your events in XAML and recreating them, once you recreate them it will reproduce the code behind.

jsmith
  • 7,198
  • 6
  • 42
  • 59
  • Thank you jsmith. At this time I don't have any events on the Xaml itself. Also, this is a small experiment to learn WPF I'm doing on my own, so I didn't use version control for this. I'm trying to avoid having to delete the XAML itself and adding a new Xaml from scratch. – FMFF Jun 11 '10 at 14:22
  • 2
    Just make a copy of the XAML code in notepad. Delete the XAML file, then recreate the XAML file, naming it the same thing, this will recreate the .cs file. Re-copy your code back into the file, and you'll have to rewrite any of your code behind if there was no version control. – jsmith Jun 11 '10 at 14:26
  • Thank you Jsmith. I tried and it solved my problem. I'm curious, isn't there a non-hack way of doing this? – FMFF Jun 11 '10 at 14:28
  • I don't feel like it's that much of a hack. I would consider the cleanest way of fixing this is reverting with version control. Since it's a learning project I understand why you didn't put it on VC. – jsmith Jun 11 '10 at 14:33
2

I know i am late to the party...but this is what just worked for me. IF you are in VisualStudio you can: 1. Exclude all the files you want to link from the project 2. Include just the .xaml file. For me that included the .xaml.cs file as well as code behind.

Note: Make sure the compile relationship is correct in the .csproj file. It should look like this:

<ItemGroup>
  <Compile Include="App.xaml.cs">
    <DependentUpon>App.xaml</DependentUpon>
  </Compile>...

To see that text you will need to unload the project and then edit the .csproj file inside any texteditor. VS 2015 also lets you edit that file if you rightclick on the unloaded .csproj file.

Hope this helps someone.

Chris Niemann
  • 43
  • 1
  • 9