wpf 当前上下文中不存在名称“InitializeComponent”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6925584/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
The name 'InitializeComponent' does not exist in the current context
提问by user876402
If I create a new project in Visual Studio 2010 SP1 and select "WPF Application" and tries to build the generated application, I get the error
如果我在 Visual Studio 2010 SP1 中创建一个新项目并选择“WPF 应用程序”并尝试构建生成的应用程序,我会收到错误消息
The name 'InitializeComponent' does not exist in the current context.
当前上下文中不存在名称“InitializeComponent”。
I got a similar error this morning when I tried to build my current project. Yesterday, I had no problem compiling and running it.
今天早上我尝试构建当前项目时遇到了类似的错误。昨天,我编译和运行它没有问题。
I created a new project and got the error whenever I compiled the project. I have just sent the project to a colleague, and he has just compiled without any errors.
我创建了一个新项目,但每次编译该项目时都会出现错误。我刚刚把项目发给了同事,他刚刚编译没有任何错误。
What is wrong?
怎么了?
回答by Sean B
I've encountered this a couple times and keep forgetting what causes it. I ran into this when I renamed the namespace on my code behind file but not in my XAML.
我遇到过几次,一直忘记是什么原因造成的。当我在代码隐藏文件中重命名命名空间时遇到了这个问题,但在我的 XAML 中没有。
So check if you've done the same.
所以检查你是否做了同样的事情。
The namespaceand classnames need to match since they are both part of a partial class
该命名空间和类名需要匹配,因为它们是一个局部类的两个组成部分
namespace ZZZ
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow
{
//...
}
}
<!-- XAML -->
<Window x:Class="ZZZ.MainWindow">
回答by Segfault
The Build Action for the .xaml file must also be set to "Page", when moving a xaml file between projects this setting gets lost (in VS 2010 at least).
.xaml 文件的构建操作也必须设置为“页面”,当在项目之间移动 xaml 文件时,此设置会丢失(至少在 VS 2010 中)。
回答by BCA
For those who have no errors in Debug mode, but dohave the specified error in Release mode (and yet the project runs fine), here is something simple to try:
对于那些谁在调试模式下没有错误,但这样做在发行模式指定的错误(,但该项目运行良好),这里是一些简单的尝试:
- Open the XAML file corresponding to the offending xaml.cs file.
- Make an edit--any edit, like add a space somewhere
- Save the file and close it
- 打开与违规 xaml.cs 文件对应的 XAML 文件。
- 进行编辑——任何编辑,比如在某处添加一个空格
- 保存文件并关闭它
This method worked for me in VS 2015, and according to other users, also 2017 and 2019
这种方法在 VS 2015 中对我有用,根据其他用户的说法,也适用于 2017 和 2019
回答by JordanBean
- Navigate to the solution directory
- Delete the \obj folder
- Rebuild the solution
- 导航到解决方案目录
- 删除 \obj 文件夹
- 重建解决方案
I encountered this error during refactoring where I renamed some files/folders and the prexisiting *.g.cs files needed to be re-generated.
我在重构期间遇到了这个错误,我重命名了一些文件/文件夹,并且需要重新生成预先存在的 *.g.cs 文件。
回答by Shailender Singh
There's a very specific reason for this, and it's in the project settings. This usually happens whenever you try to add a WPF control/window to a .NET 2.0 class library or project. The reason for this error is that the project does not know it's building a WPF control or window and therefore tries to build it as a C# 2.0 project.
这有一个非常具体的原因,它在项目设置中。每当您尝试将 WPF 控件/窗口添加到 .NET 2.0 类库或项目时,通常都会发生这种情况。此错误的原因是项目不知道它正在构建 WPF 控件或窗口,因此尝试将其构建为 C# 2.0 项目。
The solution involves editing the .csproj file. Right click on the project causing the problem and select “Unload Project”. Right click the unloaded project and select “Edit .csproj”. The .csproj file will open and you can see the XML. look for the following line:
该解决方案涉及编辑 .csproj 文件。右键单击导致问题的项目,然后选择“卸载项目”。右键单击卸载的项目并选择“编辑 .csproj”。.csproj 文件将打开,您可以看到 XML。查找以下行:
<Import Project=…..
It's near the end of the file, and the only line that you have is probably
它接近文件的末尾,您拥有的唯一一行可能是
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
This tells Visual Studio to build the project as a .NET 2.0 project. What we want to do is to tell Visual Studio that this is actually a WPF project, so we have to add the following line:
这告诉 Visual Studio 将项目构建为 .NET 2.0 项目。我们要做的是告诉 Visual Studio 这实际上是一个 WPF 项目,因此我们必须添加以下行:
<Import Project="$(MSBuildBinPath)\Microsoft.WinFX.targets" />
This line will tell Visual Studio to build the project as a WPF project. Now your .csproj file bottom should look like this:
此行将告诉 Visual Studio 将项目构建为 WPF 项目。现在您的 .csproj 文件底部应如下所示:
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<Import Project="$(MSBuildBinPath)\Microsoft.WinFX.targets" />
Save the .csproj file, right click it in Solution Explorer and select “Reload Project” compile and that's it, you're all done!
保存 .csproj 文件,在解决方案资源管理器中右键单击它并选择“重新加载项目”编译,就是这样,你就大功告成了!
回答by p3d3str1an
this happened with me when I accidentaly deleted the class reference from the xaml definition:
当我不小心从 xaml 定义中删除了类引用时,这发生在我身上:
I've replaced the
我已经更换了
<Window x:Class="myapp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
first line with this:
第一行:
<RibbonWindow
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
I know this isn't the answer to the original question (because thats project builds on another machine), but the error message was the same, so maybe I'll help someone with this situation.
我知道这不是原始问题的答案(因为那是在另一台机器上构建的项目),但错误消息是相同的,所以也许我会帮助解决这种情况的人。
回答by Reader Man San
You might get this error when you import a class from another project, or change the path of the xaml file, or the namespace of either the xaml or behind .cs file.
当您从另一个项目导入类时,或更改 xaml 文件的路径或 xaml 或 .cs 文件后面的命名空间时,您可能会收到此错误。
One:It might have a namespace that is not the same as what you have in you new project
一:它的命名空间可能与您在新项目中的命名空间不同
namespace TrainerB.MVC.Forms
{
public partial class AboutDeveloper : ContentPage
{
public AboutDeveloper()
{
InitializeComponent();
}
}
}
As you can see the name space in the imported file begins with the old project name: "TrainerB", but your new project might have a different name, so just change it to the correct new project name, in both the .xaml file and the behind .cs file.
正如您所看到的,导入文件中的命名空间以旧项目名称开头:"TrainerB",但您的新项目可能具有不同的名称,因此只需在 .xaml 文件和 .xaml 文件中将其更改为正确的新项目名称即可后面的 .cs 文件。
Two:
二:
change the properties of the .xaml file to:
将 .xaml 文件的属性更改为:
Build Action:Embedded Resource
构建操作:嵌入式资源
Custom Tool:MSBuild:UpdateDesignTimeXaml
自定义工具:MSBuild:UpdateDesignTimeXaml
回答by Song
Make sure in the xaml file
确保在 xaml 文件中
<Page x:Class="Project1.Page1" ...
match the 'Project1' name and the 'Page1' name
匹配“Project1”名称和“Page1”名称
回答by Ctrl S
Check the Designerfile.
检查设计器文件。
I had this same issue. In my case, the cause was that the namespace
for FileName.Designer.csdid not match the (correct) namespace
used in FileName.cs.
我有同样的问题。在我的情况,原因是,namespace
对于FileName.Designer.cs不匹配(正确)namespace
中使用FileName.cs。
Changing the namespace
of FileName.Designer.csto match that of FileName.cssolved the problem immediately.
改变namespace
的FileName.Designer.cs相匹配的FileName.cs立即解决了这个问题。
回答by Dave
I've had this (although it was very much my fault and was caused after I copied and pasted some code in); it can occur when the namespace doesn't match between the XAML and code behind
我有这个(虽然这很大程度上是我的错,是在我复制并粘贴一些代码之后引起的);当 XAML 和后面的代码之间的命名空间不匹配时,就会发生这种情况
EG
例如
<UserControl x:Class="DockPanel.TreeView" />
and the code behind is
后面的代码是
namespace NotDockPanel