简体   繁体   English

WPF应用程序中的当前上下文中不存在名称“InitializeComponent”

[英]The name 'InitializeComponent' does not exist in the current context in WPF application

I have checked all similar questions on StackOverflow, but none of the answers solved my problem. 我已经检查了StackOverflow上的所有类似问题,但没有一个答案解决了我的问题。 I simply get the error in title. 我只是在标题中得到错误。

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. 谢谢。

要修复它,请更改xaml的命名空间

<Window x:Class="CustomerOrder.App.MainWindow"

Your MainWindow class is not in the same namespace as the xaml . 您的MainWindow类与xaml不在同一名称空间中。 Change it to 将其更改为

namespace CodeFirstMVVM.App
{ 
      /// <summary>
      /// Interaction logic for MainWindow.xaml 
      /// </summary> 
      public partial class MainWindow : Window 
     { 
            public MainWindow()
            { 
                  InitializeComponent(); 
             }
     } 
 }

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. 修复它的原因只是重新输入“X:Class”属性。 Then VS seems to correctly parse it and make the proper association. 然后VS似乎正确地解析它并进行正确的关联。

This technique of simply retyping what ever VS is complaining about works for a number of other errors as well. 这种简单地重新输入VS抱怨的技术也适用于许多其他错误。

Very frustrating... 很沮丧......

创建xaml文件时App.Xaml文件的类名称应为NameSpace.app,而其他Windows应具有NameSpace.WindowName。

I had fiddled around with excluding and re-including xaml and corresponding files. 我已经摆弄了排除和重新包含xaml和相应的文件。

For me closing and reopening Visual Studio worked . 关闭和重新打开Visual Studio 工作

暂无
暂无

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

相关问题 我的 WPF 应用程序的当前上下文中不存在名称“InitializeComponent” - The name 'InitializeComponent' does not exist in the current context in my WPF application 名称InitializeComponent在当前上下文中不存在 - The name InitializeComponent does not exist in the current context 当前上下文中不存在“ InitializeComponent” - 'InitializeComponent' does not exist in the current context 使用Visual Studio 2015打开WPF项目后,当前上下文错误中不存在名称“ InitializeComponent” - The name 'InitializeComponent' does not exist in the current context error after opening WPF project with Visual Studio 2015 新的WPF项目无法编译-引发错误“名称&#39;InitializeComponent&#39;在当前上下文中不存在” - New WPF Project Won't Compile — Throws Error “The name 'InitializeComponent' does not exist in the current context” 当前上下文中不存在名称“InitializeComponent”(c#,xamarin) - The name 'InitializeComponent' does not exist in the current context(c#,xamarin) .NET Maui 当前上下文中不存在名称“InitializeComponent” - .NET Maui The name 'InitializeComponent' does not exist in the current context 当前上下文中不存在名称“InitializeComponent” - The name 'InitializeComponent' doesn't exist in the current context 当前上下文错误中不存在InitializeComponent - InitializeComponent does not exist in the current context error 当前上下文中不存在“InitializeComponent”和“_FrameViews” - The 'InitializeComponent' and '_FrameViews' does not exist in the current context
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM