views:

115

answers:

3

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.

+2  A: 

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
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
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
Thank you Jsmith. I tried and it solved my problem. I'm curious, isn't there a non-hack way of doing this?
FMFF
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
+1  A: 

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
+2  A: 

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.

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".

Ray Burns