Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't Visual Studio find my WPF InitializeComponent method?

Tags:

wpf

This is very strange.

I have an XAML file that looks as follows...

<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."

How can I fix this problem?

like image 288
willem Avatar asked Jun 05 '09 08:06

willem


People also ask

What does InitializeComponent do in WPF?

#91 – What InitializeComponent() Does The entry point into a WPF application, the Main function, is quite simple. It creates an instance of your Application object, calls its InitializeComponent method and then its Run method.

Is WPF discontinued?

It was in 2006 that Windows Presentation Foundation (WPF) was released with . NET framework 3.0. Over the years it got improved and it is still now in the market in 2021.

Does Visual Studio use WPF?

Windows Presentation Foundation (WPF) in Visual Studio provides developers with a unified programming model for building line-of-business desktop applications on Windows.


1 Answers

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 Action for the XAML (in Properties window) was changed to "Resource". I changed it to "Page", and it started working fine.

I hope this helps.

The thread which helped me with this was The name 'InitializeComponent' does not exist....

like image 171
gp. Avatar answered Oct 22 '22 16:10

gp.