6618

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. I simply get the error in title.

Here is my 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 :

<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 :

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.

Answer1:

To fix it, change the namespace of the xaml

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

Answer2:

Your MainWindow class is not in the same namespace as the xaml . Change it to

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

Answer3:

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.

This technique of simply retyping what ever VS is complaining about works for a number of other errors as well.

Very frustrating...

Answer4:

While Creating The xaml File The App.Xaml File Should have the class name as NameSpace.app, While Other Windows Should have NameSpace.WindowName.

Answer5:

I had fiddled around with excluding and re-including xaml and corresponding files.

For me <strong>closing and reopening Visual Studio</strong> worked.

Recommend

  • Behaviors in UWP (Event trigger command error
  • WPF - Style validation error of UserControl
  • Changing the background color of a metro style app has no effect
  • Silverlight Image Flip Animation
  • Why is the brush preventing dc.js barChart toolTips to appear?
  • See UI changes in design view with WPF & XAML and data binding?
  • Click event in iframe
  • Binding a Storyboard Animation inside a DataTrigger crashes the XamlParser
  • What is the use of “arguments[0]” when implementing javascriptexecutor?
  • Silverlight UI not unsubscribing from PropertyChanged events
  • WP 7.1 app shows thich Black header while running in WP8 emulator
  • Continue if element is not visible in protractor
  • Interactive Design-Time User Control
  • Bind events to Item ViewModel
  • How to change EasingDoubleKeyFrame value at Runtime?
  • Flyout.showAt() ArgumentException based on Content
  • DataGrid: Help accessing a control defined in a CellEditingTemplate
  • Azure Media Services Shared Access Policy limitations
  • Visualizing 2D Data in a Table
  • .NET Core IHttpContextAccessor issue
  • Binding from within a ResourceDictionary in a Catel WPF UserControl
  • Different Datacontext for Command and CommandParameter
  • Return null in boolean to checkbox state converter in XAML
  • Caliburn Micro, How to use ContentControl (or display 'sub' ViewModel) using ViewModel Fir
  • Drawing a Path in XAML (with binding) vs. C#. One works, one doesn't
  • Consuming a WCF service in a Java Client using wsHttpBinding
  • Force show.bind execution
  • Listbox within Listbox and scrolling trouble in Windows Phone 7 Silverlight
  • Checking free space on FTP server
  • Get one-time binding to work for ng-if
  • Nant, Vault & Windows Integrated Authentication
  • Counter field in MS Access, how to generate?
  • script to move all files from one location to another location
  • ILMerge & Keep Assembly Name
  • Javascript + PHP Encryption with pidCrypt
  • Symfony2: How to get request parameter
  • Websockets service method fails during R startup
  • QuartzCore.framework for Mono Develop
  • Run Powershell script from inside other Powershell script with dynamic redirection to file
  • Why can't I rebase on to an ancestor of source changesets if on a different branch?