为什么 Visual Studio 找不到我的 WPF InitializeComponent 方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/954861/
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
Why can't Visual Studio find my WPF InitializeComponent method?
提问by willem
This is very strange.
这很奇怪。
I have an XAMLfile that looks as follows...
我有一个XAML文件,如下所示...
<Window
x:Name="window"
x:Class="ix.Production.Title"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Title" Height="768" Width="1024"
Loaded="window_Loaded">
<Window.Resources>
etc...
And my code-beside that looks as follows...
我的代码旁边看起来如下......
using System;
using System.Windows;
using System.Windows.Media.Animation;
using System.Threading;
namespace ix.Production
{
public partial class Title : Window
{
public Title()
{
InitializeComponent();
}
....
This code refuses to compile because Visual Studio insists that the InitializeComponent "does not exist in the current context."
此代码拒绝编译,因为 Visual Studio 坚持认为 InitializeComponent“在当前上下文中不存在”。
How can I fix this problem?
我该如何解决这个问题?
采纳答案by willem
Looks like this was a kink in Visual Studio 2008.
看起来这是 Visual Studio 2008 中的一个问题。
If I create a new file (i.e. Title2), copy/paste the code and XAML, then change all 'Title' to 'Title2', everything works fine again.
如果我创建一个新文件(即 Title2),复制/粘贴代码和 XAML,然后将所有“Title”更改为“Title2”,则一切正常。
回答by gp.
One case I have seen this happening in when you copy paste an XAML control/window, etc. The InitializeComponent method exists in a corresponding .g.cs file that is automatically generated. In my case, after copy paste, Build Actionfor the XAML (in Properties window) was changed to "Resource". I changed it to "Page", and it started working fine.
我在复制粘贴 XAML 控件/窗口等时看到过这种情况。 InitializeComponent 方法存在于自动生成的相应 .g.cs 文件中。就我而言,复制粘贴后,XAML 的构建操作(在“属性”窗口中)更改为“资源”。我将其更改为 "Page",它开始正常工作。
I hope this helps.
我希望这有帮助。
The thread which helped me with this was The name 'InitializeComponent' does not exist....
帮助我解决这个问题的线程是The name 'InitializeComponent' does not exist...。
回答by Bojan Resnik
Your XAML says:
你的 XAML 说:
x:Class="ix.Production.Title"
while the actual class is ix.Outage.Title
. Either change the XAML declaration or move the class to the ix.Production
namespace.
而实际的类是ix.Outage.Title
. 更改 XAML 声明或将类移动到ix.Production
命名空间。
回答by DavidDraughn
So simple, but so easy to overlook: MainWindow
's x:Class
was set to "MainWindow
", when it should have been set to namespace.MainWindow
(x:Class="namespace.MainWindow"
). If this is not done, then the MainWindow.g.xaml
file will not include a namespace block, and the call to InitializeComponent()
in MainWindow
's constructor will not work.
如此简单,但如此容易被忽略:MainWindow
'sx:Class
被设置为“ MainWindow
”,而它本应设置为namespace.MainWindow
( x:Class="namespace.MainWindow"
)。如果不这样做,则该MainWindow.g.xaml
文件将不包含命名空间块,并且对InitializeComponent()
inMainWindow
的构造函数的调用将不起作用。
Although this obviously isn't a solution to willem's problem, I posted it here because there are so many other solutions listed. Hopefully this will help someone else who stumbles on this post when trying to fix their problem.
尽管这显然不是 willem 问题的解决方案,但我将其发布在这里是因为列出了许多其他解决方案。希望这能帮助那些在尝试解决问题时偶然发现这篇文章的人。
回答by rmoore
Your namespaces don't match:
您的命名空间不匹配:
x:Class="ix.Production.Title"
namespace ix.Outage { ...
回答by Andrei Floroiu
I usually get this problem when renaming custom control classes, or, as in your case, changing the name of the class's namespace. If you don't use Visual Studio's refactoring, then you have to manually change the names in both XAML and C# files.
我通常在重命名自定义控件类时遇到这个问题,或者在你的情况下,更改类的命名空间的名称。如果不使用 Visual Studio 的重构,则必须手动更改 XAML 和 C# 文件中的名称。
Refactoring is only good when you just rename the class, for namespace rename, you have to do it manually.
重构只有在您重命名类时才有用,对于命名空间重命名,您必须手动进行。
For InitializeComponent
, look in [Project Folder]\obj\Debug\<class_name.g.cs>
.
对于InitializeComponent
,请看[Project Folder]\obj\Debug\<class_name.g.cs>
。
回答by Peter pete
I become unable to find InitializeComponent, and other XAML-related UI bits from the code behind the moment I add my PCL (which contains my data classes) into the solution.
当我将我的 PCL(包含我的数据类)添加到解决方案中时,我无法从背后的代码中找到 InitializeComponent 和其他与 XAML 相关的 UI 位。
:|
:|
But if I remove the PCL one .. it can be found again.
但是,如果我删除 PCL 之一 .. 它可以再次找到。
Regardless, whether its found or not - I can still compile. I just can't autocomplete when i type code.
无论如何,无论是否找到 - 我仍然可以编译。我只是在输入代码时无法自动完成。
Maybe you have a PCL project in your solution? (I don't know how to fix this yet)
也许您的解决方案中有一个 PCL 项目?(我还不知道如何解决这个问题)
回答by Yngvar Johnsen
I had the same problem with a Visual Studio 2010 sample project that I manually reverted to Visual Studio 2008. I found I had forgotten to set the Target Framework of the project to .NET Framework 3.5. It was empty in the project reverted to Visual Studio 2008, initially set to 4.0 in the original Visual Studio 2010 project.
我在手动恢复到 Visual Studio 2008 的 Visual Studio 2010 示例项目中遇到了同样的问题。我发现我忘记将项目的目标框架设置为 .NET Framework 3.5。它在恢复到 Visual Studio 2008 的项目中为空,在原始 Visual Studio 2010 项目中最初设置为 4.0。
To set Target Framework of a project, go to project properties, access the first tab called Application, and select .NET Framework 3.5 in the Target Framework dropdown.
要设置项目的目标框架,请转到项目属性,访问名为Application的第一个选项卡,然后在目标框架下拉列表中选择 .NET Framework 3.5。
Of course, I'd prefer Visual Studio giving me an error or at least a warning that I had not set my Target Framework, but hey, that's part of developing in Visual Studio, I guess.
当然,我更喜欢 Visual Studio 给我一个错误或至少警告我没有设置我的目标框架,但是嘿,我猜这是在 Visual Studio 中开发的一部分。
回答by Ira
This happened to me because Visual Studio sets the "Build action" to some random actionwhen you copy/paste or move an XAML file from one project to another within a solution.
这发生在我身上,因为当您在解决方案中将XAML 文件从一个项目复制/粘贴或移动到另一个项目时,Visual Studio 将“构建操作”设置为某个随机操作。
Solution: Make sure your "Build action" is set to page for you XAML.
解决方案:确保您的“构建操作”设置为 XAML 的页面。
I hope this helps if it's the same case for you.
如果您的情况相同,我希望这会有所帮助。
回答by Guenther
When namespaces are all correct changing the target platform from x86 to x64 or vice versa and rebuilding the project often does the trick.
当命名空间都正确时,将目标平台从 x86 更改为 x64,反之亦然,并且重建项目通常可以解决问题。
回答by Frederik Gheysels
Your class is partial, so you should have another file that contains some other parts of your Title class (the InitializeComponent method for instance). Try to find that file, and see if the namespace in that file, is equal to the namespace of the file which contains the other parts of your class.
您的类是部分类,因此您应该有另一个文件,其中包含 Title 类的一些其他部分(例如 InitializeComponent 方法)。尝试找到该文件,并查看该文件中的命名空间是否等于包含类其他部分的文件的命名空间。