xaml.cs 个文件的 InitializeComponent() 出错

getting an error on InitializeComponent() on xaml.cs files

我有时会收到一个红色错误:

InitializeComponent does not exist in the current context

连同我在 .xaml 中的其他变量,这些变量受 x:Name 约束。 Xaml 文件中的 x:Class 确实对应于我的 xaml.cs 文件中的命名空间和 class 名称。

我的 xaml 文件的构建操作设置为 "Embedded Resource",我的 xaml.cs 文件的构建操作设置为 "Compile"。该错误只是随机出现,即使出现红色错误,项目也能正常运行。我正在 windows 8.1 上使用 Visual Studio 和 Xamarin 进行开发。

即使代码运行,我也不希望看到这些错误出现。发生这种情况有什么原因吗?

如果您使用的是共享项目,则无法让这些 "errors" 消失。 SAP 无法识别 InitializeComponent() 方法,因此将其标记为错误,因为后面的代码无法识别 x:Name 直到您 运行 它。

编辑 2015 年 8 月 11 日

PCL 不应该给你这种错误,检查你的 xaml 命名空间 x:Class 和你的 xaml.cs (代码隐藏)命名空间是否相同.

您可以做的另一件事是将您的 Xamarin.Forms 参考更新为最新版本(包括预发布版本)。

这对我有帮助:

In package manager console, issue the following command:

update-package -project your_pcl_project_name -reinstall

https://social.msdn.microsoft.com/Forums/en-US/e6d4e336-b3b6-45ef-b681-87c351f902e9/xamarin-forms-project-xaml-compilation-error-in-vs15-community-with-update-2-installed?forum=visualstudiogeneral#answers