Accidentally deleted the code for the Xaml file. How to add code again?

Instead of an exception, by mistake I deleted the .cs code behind the file for the Xaml file. Now I do not know how to add code behind.

This window is empty, it does not have user interface controls. "Show code" is disabled, and I can not see events (lightning bolt icon) anywhere for this xaml.

Please, help.

+7
c # wpf xaml code-behind
source share
4 answers

As far as I know without Version Control , there is no way to go back if the project was saved. You should look at your version control code.

At this point, I will consider reconstructing your xaml file, copying the old code, then deleting your events in XAML and re-creating them, as soon as you recreate them, it will play the code behind.

+2
source share

There are two required steps and one optional:

  • Create a new .xaml.cs file in the same directory (right-click the project โ†’ Add โ†’ New item โ†’ Class)
  • Copy the template code from another .xaml.cs into your project, changing the class name accordingly (for example, copy the "using" directives, class declaration and constructor, including calling InitializeComponent).
  • (optional) Edit the .csproj file to add the <DependentUpon> element below your <Compile> element for the .xaml.cs file so that it is not โ€œbelowโ€ the .xaml file. If you are updating a project for editing with Blend (v4), this third step is required, and not necessary, to use the inspector to add events to the controls.

To easily edit the .csproj file:

  • Right-click the project and select Upload Project
  • Right-click the node project again and select "Change [project name] .csproj"
  • Edit the XML, then close the file
  • Right-click the project node and select "Update Project"

If you use VB.NET, everything is the same, just replace "cs" with "vb".

+14
source share

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

test.xaml

test.xaml.cs

works in vb ....

+3
source share

I know I'm late for the party ... but this is what just helped me. If you are in VisualStudio, you can: 1. Exclude all files that you want to link to the project. 2. Include only the .xaml file. For me, this included the .xaml.cs file as well as the code.

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

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

To see this text, you will need to unload the project, and then edit the .csproj file inside any text editor. VS 2015 also allows you to edit this file if you are right in an unloaded .csproj file.

Hope this helps someone.

+2
source share

All Articles