简体   繁体   English

意外删除了Xaml文件背后的代码。 如何再次添加代码?

[英]Accidentally deleted the code behind for an Xaml file. How do I add the code behind again?

Instead of excluding it, by mistake, I deleted the .cs code behind file for an Xaml file. 我没有错误地排除它,而是删除了文件后面的.cs代码以获取Xaml文件。 Now, I don't know how to add the code behind. 现在,我不知道如何添加代码。

This Window is empty with no UI controls on it. 此窗口为空,没有UI控件。 "View Code" is disabled and I can't see the Events (lightning bolt icon) anywhere for this xaml. “查看代码”已禁用,我无法在此xaml的任何位置看到事件(闪电图标)。

Please help. 请帮忙。

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) 在同一目录中创建一个新的.xaml.cs文件(右键单击项目 - >添加 - >新项 - >类)
  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). 从项目中的其他.xaml.cs复制样板代码,适当地更改类名(例如,复制“using”指令,类声明和构造函数,包括InitializeComponent调用)。
  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. (可选)编辑.csproj文件,在.xaml.cs文件的<Compile> <DependentUpon>元素下面添加一个<DependentUpon>元素,使其显示在.xaml文件的“内部”,而不是简单地在其下面。 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. 如果要使用Blend(v4)更新要编辑的项目,则需要执行此第三步,而不是可选步骤,以便使用检查器向控件添加事件。

To easily edit the .csproj file: 要轻松编辑.csproj文件:

  1. Right-click the project and select "Unload Project" 右键单击该项目,然后选择“卸载项目”
  2. Right-click the project node again and select "Edit [projectname].csproj" 再次右键单击项目节点,然后选择“编辑[projectname] .csproj”
  3. Edit the XML, then close the file 编辑XML,然后关闭文件
  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". 如果您使用的是VB.NET,那么一切都相同,只需将“cs”替换为“vb”即可。

I think all you have to do is create a new class file and name it exactly like your xaml with the cs extension... 我认为您所要做的就是创建一个新的类文件,并使用cs扩展名将其命名为xaml ...

test.xaml test.xaml

test.xaml.cs test.xaml.cs

works in vb.... 适用于vb ....

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. 您应该考虑将代码放在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. 此时我会考虑重新创建你的xaml文件,复制你的旧代码,然后在XAML中删除你的事件并重新创建它们,一旦你重新创建它们,它将重现后面的代码。

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. 如果您在VisualStudio中,您可以:1。从项目中排除要链接的所有文件2.仅包括.xaml文件。 For me that included the .xaml.cs file as well as code behind. 对我来说,包括.xaml.cs文件以及后面的代码。

Note: Make sure the compile relationship is correct in the .csproj file. 注意:确保.csproj文件中的编译关系正确。 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. 要查看该文本,您需要卸载项目,然后编辑任何texteditor内的.csproj文件。 VS 2015 also lets you edit that file if you rightclick on the unloaded .csproj file. 如果右键单击卸载的.csproj文件,VS 2015还允许您编辑该文件。

Hope this helps someone. 希望这有助于某人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM