简体   繁体   English

c# 不包含“InitializeComponent”的定义,名称“controlName”在当前上下文中不存在

[英]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.自 6 月以来,我一直在团队中开发 windows 电话应用程序。 Everything worked fine with all the syncs until today.直到今天,所有同步都运行良好。 I synced the project and vs started giving me the errors我同步了项目,vs 开始给我错误

  • "WindowsPhoneApp.MainPage" does not contain a definition for InitializeComponent (and in all other pages, even App) “WindowsPhoneApp.MainPage”不包含 InitializeComponent 的定义(以及所有其他页面,甚至 App)
  • The name "controlName" does not exist in the current context (this happens in all the pages)当前上下文中不存在名称“controlName”(这发生在所有页面中)

I didn't change anything, it worked fine yesterday.我没有改变任何东西,昨天它运行良好。 Classes names on XAML match the names in code-behind. XAML 上的类名称与代码隐藏中的名称匹配。
I've tried exited all vs instances but the problems are still there.我已经尝试退出所有 vs 实例,但问题仍然存在。

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: 上述页面构造函数中的Page1.xaml.cs的错误:

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

Xaml file XAML文件

<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" 应该是x:Class="App1.Views.Page1"

so just typo in the class name. 因此,只需在班级名称中输入错字即可。

Close the solution, delete the.suo file associated with the solution, delete the "bin" and the "obj" folders in all projects.关闭解决方案,删除与解决方案关联的.suo文件,删除所有项目中的“bin”和“obj”文件夹。 Then re-open the solution in VS and rebuild the solution.然后在VS中重新打开解决方案,重建解决方案。

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. “suo”文件是一个隐藏文件,因此您可能需要在资源管理器中按 go 到菜单/查看,然后勾选“隐藏项目”选项。 In VS2013 it was in the same folder as the.sln file, in newer versions it's in the (hidden).vs folder.在 VS2013 中,它与 .sln 文件位于同一文件夹中,在较新的版本中,它位于(隐藏).vs 文件夹中。

For me, the problem was app was running in background.对我来说,问题是应用程序在后台运行。

There was no app window or icon in the taskbar.任务栏中没有应用程序 window 或图标。 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.即使 Visual Studio 关闭,应用程序实例仍然存在,阻止我手动清除 Solution 中的/bin/obj ,因为 Windows 抛出了“当前正在使用的文件”异常。

Solution is to解决办法是

  • Kill the app using Task Manager ( CTRL + SHIFT + ESC )使用任务管理器 ( CTRL + SHIFT + ESC ) 终止应用程序
  • Clean the solution清洗溶液
  • Rebuild app重建应用程序

Restarting the system will also work.重新启动系统也将起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当前上下文中不存在名称“InitializeComponent”(c#,xamarin) - The name 'InitializeComponent' does not exist in the current context(c#,xamarin) 当前上下文中不存在名称“controlname” - The name 'controlname' does not exist in the current context 名称InitializeComponent在当前上下文中不存在 - The name InitializeComponent does not exist in the current context InitializeComponent 在当前上下文中不存在,使用 Mono C# 编译器 - InitializeComponent does not exist in the current context, using Mono C# compiler 当前上下文中不存在“ InitializeComponent” - 'InitializeComponent' does not exist in the current context 我的 WPF 应用程序的当前上下文中不存在名称“InitializeComponent” - The name 'InitializeComponent' does not exist in the current context in my WPF application WPF应用程序中的当前上下文中不存在名称“InitializeComponent” - The name 'InitializeComponent' does not exist in the current context in WPF application .NET Maui 当前上下文中不存在名称“InitializeComponent” - .NET Maui The name 'InitializeComponent' does not exist in the current context C# - 当前上下文中不存在该名称 - C# - The name does not exist in the current context 该名称在当前上下文中不存在c# - The name does not exist in the current context c#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM