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

Viewed 52120

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?

14 Answers

Your XAML says:

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.

Your namespaces don't match:

x:Class="ix.Production.Title"
namespace ix.Outage { ...

Looks like this was a kink in 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.

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.

When namespaces are all correct changing the target platform from x86 to x64 or vice versa and rebuilding the project often does the trick.

Related