'c# does not contain a definition for "InitializeComponent" and name "controlName" does not exist in current context

I've been developing a windows phone app in a team since June. Everything worked fine with all the syncs until today. I synced the project and vs started giving me the errors

  • "WindowsPhoneApp.MainPage" does not contain a definition for InitializeComponent (and in all other pages, even App)
  • The name "controlName" does not exist in the current context (this happens in all the pages)

I didn't change anything, it worked fine yesterday. Classes names on XAML match the names in code-behind.
I've tried exited all vs instances but the problems are still there.



Solution 1:[1]

For me, the problem was following:


Project structure

Project\Views\Page1.xaml
Project\Views\Page1.xaml.cs

The error in Page1.xaml.cs on abovementioned page constructor:

public sealed partial class Page1 : Page
{
    public Page1()
    {
        this.InitializeComponent();
    }
}

Xaml file

<Page
    x:Class="App1.Page1"
    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"
    mc:Ignorable="d">

should be x:Class="App1.Views.Page1"

so just typo in the class name.

Solution 2:[2]

Close the solution, delete the .suo file associated with the solution, delete the "bin" and the "obj" folders in all projects. Then re-open the solution in VS and rebuild the solution.

The "suo" file is a hidden file so you may need to go in the explorer to menu / View and there check the "Hidden items" option. In VS2013 it was in the same folder as the .sln file, in newer versions it's in the (hidden) .vs folder.

Solution 3:[3]

For me, the problem was app was running in background.

There was no app window or icon in the taskbar. The application instance persisted even when Visual Studio was closed, preventing me from manually clearing /bin and /obj in Solution because Windows threw 'files currently in use' exception.

Solution is to

  • Kill the app using Task Manager (CTRL + SHIFT + ESC)
  • Clean the solution
  • Rebuild app

Restarting the system will also work.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Arsinclair
Solution 2 Robin Bennett
Solution 3 Az21