17

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.

Usr
  • 2,628
  • 10
  • 51
  • 91
  • genrally intializeComponenet are auto generated methods where your controls are defined which resides in designer.cs file...may be while syncing you deleted some designer file code or designer file itself!! – Viru Sep 27 '15 at 10:13
  • 1
    Try closing the solution, delete the hidden .suo file in your solution directory and open the solution again. Don't know if that works for you, but I experience the same issue with silverlight in VS2013. – Silvermind Sep 27 '15 at 10:13
  • @Silvermind where exactly can I find that file? – Usr Sep 27 '15 at 10:19
  • 10
    Close the solution, delete the suo file associated with the solution, delete the "bin" and the "obj" folders in all projects. Then open the solution in VS and rebuild the solution. The "suo" file is in the same folder as the "sln" file. That is in the root folder for the solution. It is a hidden file so you may need to go in the explorer to menu / View and there check the "Hidden items" option. – Ladi Sep 27 '15 at 10:21

3 Answers3

15

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.

Raman Sinclair
  • 1,194
  • 17
  • 31
  • 3
    I know that it is obvious, but may be helpful for anyone. This typo "ate" 80 min of my time. – Raman Sinclair Nov 23 '17 at 18:02
  • Just had the same issue. Thanks for posting your solution. – Jimmy Feb 07 '19 at 19:03
  • Usually, for me, it is the wrong user defined tool (should be "MSBuild:UpdateDesignTimeXaml") and "build action" ("embedded resource")... – thomiel May 06 '21 at 14:27
  • but today it was a weird situation till I connected to my Mac build machine -- and Intellisense still insists that InitializeComponent isn't defined. – thomiel May 06 '21 at 14:41
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.

Robin Bennett
  • 3,192
  • 1
  • 8
  • 18
0

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.

Az21
  • 1