WPF 应用程序的当前上下文中不存在名称“InitializeComponent”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24693000/
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 in WPF application
提问by jason
I have checked all similar questions on StackOverflow, but none of the answers solved my problem. I simply get the error in title.
我已经在 StackOverflow 上检查了所有类似的问题,但没有一个答案解决了我的问题。我只是在标题中得到错误。
Here is my MainVindow.xaml :
这是我的 MainVindow.xaml :
<Window x:Class="CodeFirstMVVM.App.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:cm="clr-namespace:System.ComponentModel;assembly=System"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:vm="clr-namespace:CustomerOrder.App.ViewModel"
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
mc:Ignorable="d"
DataContext="{Binding Source={StaticResource Locator}, Path=CustomerView}"
Title="MainWindow" Height="500" Width="900">
<Grid>
<Canvas>
<TextBox Height="23" Canvas.Left="131" TextWrapping="Wrap" Canvas.Top="51" Width="283" Name="txtName" Text="{Binding NameUI}"/>
<DataGrid x:Name="maingrid" ItemsSource="{Binding Entities, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" SelectedItem="{Binding SelectedEntity}" AutoGenerateColumns="True" Canvas.Left="10" Canvas.Top="265">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Name}" Header="Name" Width="200"></DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
<DataGrid x:Name="ordergrid" ItemsSource="{Binding ElementName=maingrid, Path=SelectedItem.Orders}" AutoGenerateColumns="True" Canvas.Top="265" Canvas.Left="597">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Explanation}" Header="Orders" Width="200"></DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
</Canvas>
</Grid>
</Window>
And here is my App.xaml :
这是我的 App.xaml :
<Application x:Class="CustomerOrder.App.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d ="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc ="http://schemas.openxmlformats.org/markup-compatibility/2006"
StartupUri="MainWindow.xaml"
mc:Ignorable="d">
<Application.Resources>
<vm:ViewModelLocator x:Key="Locator" d:IsDataSource="True" xmlns:vm="clr-namespace:CustomerOrder.App.ViewModel" />
</Application.Resources>
</Application>
On MainWindow.xaml.cs :
在 MainWindow.xaml.cs 上:
namespace CustomerOrder.App
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
}
}
Can you tell me how to fix this? Thanks.
你能告诉我如何解决这个问题吗?谢谢。
回答by Yuliam Chandra
To fix it, change the namespace of the xaml
要修复它,请更改 xaml 的命名空间
<Window x:Class="CustomerOrder.App.MainWindow"
回答by Tzah Mama
Your MainWindow class is not in the same namespace as the xaml . Change it to
您的 MainWindow 类与 xaml 不在同一个命名空间中。将其更改为
namespace CodeFirstMVVM.App
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
}
}
回答by Harold Chattaway
I have gotten this error a number of times when all of the above checks were done and all the namespaces matched.
在完成上述所有检查并且所有命名空间都匹配时,我多次收到此错误。
What fixed it, was simply retyping the "X:Class" attribute over again. Then VS seems to correctly parse it and make the proper association.
修复它的方法只是重新输入“X:Class”属性。然后 VS 似乎正确解析它并进行适当的关联。
This technique of simply retyping what ever VS is complaining about works for a number of other errors as well.
这种简单地重新输入 VS 所抱怨的内容的技术也适用于许多其他错误。
Very frustrating...
很郁闷...
回答by Morse
I had fiddled around with excluding and re-including xaml and corresponding files.
我已经在排除和重新包含 xaml 和相应的文件方面做了很多尝试。
For me closing and reopening Visual Studioworked.
对我来说,关闭并重新打开 Visual Studio有效。
回答by Morse
While Creating The xaml File The App.Xaml File Should have the class name as NameSpace.app, While Other Windows Should have NameSpace.WindowName.
创建 xaml 文件时,App.Xaml 文件的类名应为 NameSpace.app,而其他 Windows 应具有 NameSpace.WindowName。

