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

前端 未结 14 1310
失恋的感觉
失恋的感觉 2020-12-16 08:49

This is very strange.

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



        
相关标签:
14条回答
  • 2020-12-16 09:36

    FWIW, I hit the same problem today after migration of a Visual Studio 2008 solution to Visual Studio 2010. It appears that the migration goofed and the projects lost their Target Framework (in Project Properties): The setting is now empty!?

    Setting .NET Framework 3.5 solved the problem.

    0 讨论(0)
  • 2020-12-16 09:37

    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.

    0 讨论(0)
  • 2020-12-16 09:38

    I usually get this problem when renaming custom control classes, or, as in your case, changing the name of the class's namespace. If you don't use Visual Studio's refactoring, then you have to manually change the names in both XAML and C# files.

    Refactoring is only good when you just rename the class, for namespace rename, you have to do it manually.

    For InitializeComponent, look in [Project Folder]\obj\Debug\<class_name.g.cs>.

    0 讨论(0)
  • 2020-12-16 09:38

    I was strugling with the same issue here. What I did was to delete ALL the created files unde the debug folder, then rebuild the solution.

    Regards, Petrus

    0 讨论(0)
  • 2020-12-16 09:42

    Your namespaces don't match:

    x:Class="ix.Production.Title"
    namespace ix.Outage { ...
    
    0 讨论(0)
  • 2020-12-16 09:43

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

    0 讨论(0)
提交回复
热议问题